-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set up subclassing for common controls
Subclass existing controls so we can get initial messages (such as WM_CREATE). Port BtnLook sample from WInterop.
- Loading branch information
1 parent
85ea173
commit 26657f4
Showing
12 changed files
with
393 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<OutputType>WinExe</OutputType> | ||
<AllowUnsafeBlocks>True</AllowUnsafeBlocks> | ||
<ApplicationManifest>app.manifest</ApplicationManifest> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\..\thirtytwo\thirtytwo.csproj" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
// Copyright (c) Jeremy W. Kuhne. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Drawing; | ||
using Windows; | ||
using Windows.Win32; | ||
using Windows.Win32.Foundation; | ||
|
||
namespace OwnDraw; | ||
|
||
/// <summary> | ||
/// Sample from Programming Windows, 5th Edition. | ||
/// Original (c) Charles Petzold, 1998 | ||
/// Figure 9-3, Pages 375-380. | ||
/// </summary> | ||
internal static class Program | ||
{ | ||
[STAThread] | ||
private static void Main() => Application.Run(new OwnerDraw("Owner-Draw Button Demo")); | ||
|
||
private class OwnerDraw : MainWindow | ||
{ | ||
private HWND _hwndSmaller, _hwndLarger; | ||
private int _cxClient, _cyClient; | ||
private int _btnWidth, _btnHeight; | ||
private Size _baseUnits; | ||
private const int ID_SMALLER = 1; | ||
private const int ID_LARGER = 2; | ||
|
||
public OwnerDraw(string title) : base(title) | ||
{ | ||
} | ||
|
||
protected override LRESULT WindowProcedure(HWND window, MessageType message, WPARAM wParam, LPARAM lParam) | ||
{ | ||
switch (message) | ||
{ | ||
case MessageType.Create: | ||
int baseUnits = Interop.GetDialogBaseUnits(); | ||
_baseUnits = new(baseUnits & 0xFFFF, baseUnits >> 16); | ||
_btnWidth = _baseUnits.Width * 8; | ||
_btnHeight = _baseUnits.Height * 4; | ||
|
||
// Create the owner-draw pushbuttons | ||
_hwndSmaller = new ButtonControl( | ||
style: WindowStyles.Child | WindowStyles.Visible, | ||
buttonStyle: ButtonControl.Styles.OwnerDrawn, | ||
parentWindow: this, | ||
buttonId: ID_SMALLER); | ||
_hwndLarger = new ButtonControl( | ||
style: WindowStyles.Child | WindowStyles.Visible, | ||
buttonStyle: ButtonControl.Styles.OwnerDrawn, | ||
parentWindow: this, | ||
buttonId: ID_LARGER); | ||
|
||
return (LRESULT)0; | ||
|
||
case MessageType.Size: | ||
_cxClient = lParam.LOWORD; | ||
_cyClient = lParam.HIWORD; | ||
|
||
// Move the buttons to the new center | ||
_hwndSmaller.MoveWindow( | ||
new Rectangle(_cxClient / 2 - 3 * _btnWidth / 2, _cyClient / 2 - _btnHeight / 2, _btnWidth, _btnHeight), | ||
repaint: true); | ||
_hwndLarger.MoveWindow( | ||
new Rectangle(_cxClient / 2 + _btnWidth / 2, _cyClient / 2 - _btnHeight / 2, _btnWidth, _btnHeight), | ||
repaint: true); | ||
return (LRESULT)0; | ||
|
||
case MessageType.Command: | ||
Rectangle rc = window.GetWindowRectangle(); | ||
|
||
// Make the window 10% smaller or larger | ||
switch ((int)(uint)wParam) | ||
{ | ||
case ID_SMALLER: | ||
rc.Inflate(rc.Width / -10, rc.Height / -10); | ||
break; | ||
case ID_LARGER: | ||
rc.Inflate(rc.Width / 10, rc.Height / 10); | ||
break; | ||
} | ||
|
||
window.MoveWindow(rc, repaint: true); | ||
return (LRESULT)0; | ||
|
||
case MessageType.DrawItem: | ||
|
||
var drawItemMessage = new Message.DrawItem(lParam); | ||
|
||
// Fill area with white and frame it black | ||
using (DeviceContext dc = drawItemMessage.DeviceContext) | ||
{ | ||
Rectangle rect = drawItemMessage.ItemRectangle; | ||
|
||
dc.FillRectangle(rect, StockBrush.White); | ||
dc.FrameRectangle(rect, StockBrush.Black); | ||
|
||
// Draw inward and outward black triangles | ||
int cx = rect.Right - rect.Left; | ||
int cy = rect.Bottom - rect.Top; | ||
|
||
Point[] pt = new Point[3]; | ||
|
||
switch ((int)drawItemMessage.ControlId) | ||
{ | ||
case ID_SMALLER: | ||
pt[0].X = 3 * cx / 8; pt[0].Y = 1 * cy / 8; | ||
pt[1].X = 5 * cx / 8; pt[1].Y = 1 * cy / 8; | ||
pt[2].X = 4 * cx / 8; pt[2].Y = 3 * cy / 8; | ||
Triangle(dc, pt); | ||
pt[0].X = 7 * cx / 8; pt[0].Y = 3 * cy / 8; | ||
pt[1].X = 7 * cx / 8; pt[1].Y = 5 * cy / 8; | ||
pt[2].X = 5 * cx / 8; pt[2].Y = 4 * cy / 8; | ||
Triangle(dc, pt); | ||
pt[0].X = 5 * cx / 8; pt[0].Y = 7 * cy / 8; | ||
pt[1].X = 3 * cx / 8; pt[1].Y = 7 * cy / 8; | ||
pt[2].X = 4 * cx / 8; pt[2].Y = 5 * cy / 8; | ||
Triangle(dc, pt); | ||
pt[0].X = 1 * cx / 8; pt[0].Y = 5 * cy / 8; | ||
pt[1].X = 1 * cx / 8; pt[1].Y = 3 * cy / 8; | ||
pt[2].X = 3 * cx / 8; pt[2].Y = 4 * cy / 8; | ||
Triangle(dc, pt); | ||
break; | ||
case ID_LARGER: | ||
pt[0].X = 5 * cx / 8; pt[0].Y = 3 * cy / 8; | ||
pt[1].X = 3 * cx / 8; pt[1].Y = 3 * cy / 8; | ||
pt[2].X = 4 * cx / 8; pt[2].Y = 1 * cy / 8; | ||
Triangle(dc, pt); | ||
pt[0].X = 5 * cx / 8; pt[0].Y = 5 * cy / 8; | ||
pt[1].X = 5 * cx / 8; pt[1].Y = 3 * cy / 8; | ||
pt[2].X = 7 * cx / 8; pt[2].Y = 4 * cy / 8; | ||
Triangle(dc, pt); | ||
pt[0].X = 3 * cx / 8; pt[0].Y = 5 * cy / 8; | ||
pt[1].X = 5 * cx / 8; pt[1].Y = 5 * cy / 8; | ||
pt[2].X = 4 * cx / 8; pt[2].Y = 7 * cy / 8; | ||
Triangle(dc, pt); | ||
pt[0].X = 3 * cx / 8; pt[0].Y = 3 * cy / 8; | ||
pt[1].X = 3 * cx / 8; pt[1].Y = 5 * cy / 8; | ||
pt[2].X = 1 * cx / 8; pt[2].Y = 4 * cy / 8; | ||
Triangle(dc, pt); | ||
break; | ||
} | ||
|
||
// Invert the rectangle if the button is selected | ||
if (drawItemMessage.ItemState.HasFlag(Message.DrawItem.States.Selected)) | ||
{ | ||
dc.InvertRectangle(rect); | ||
} | ||
|
||
if (drawItemMessage.ItemState.HasFlag(Message.DrawItem.States.Focus)) | ||
{ | ||
rect = Rectangle.FromLTRB( | ||
rect.Left + cx / 16, | ||
rect.Top + cy / 16, | ||
rect.Right - cx / 16, | ||
rect.Bottom - cy / 16); | ||
|
||
dc.DrawFocusRectangle(rect); | ||
} | ||
} | ||
|
||
return (LRESULT)0; | ||
} | ||
|
||
static void Triangle(DeviceContext dc, Point[] pt) | ||
{ | ||
dc.SelectObject(StockBrush.Black); | ||
dc.Polygon(pt); | ||
dc.SelectObject(StockBrush.White); | ||
} | ||
|
||
return base.WindowProcedure(window, message, wParam, lParam); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1"> | ||
<assemblyIdentity version="1.0.0.0" name="MyApplication.app"/> | ||
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2"> | ||
<security> | ||
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3"> | ||
<!-- UAC Manifest Options | ||
If you want to change the Windows User Account Control level replace the | ||
requestedExecutionLevel node with one of the following. | ||
<requestedExecutionLevel level="asInvoker" uiAccess="false" /> | ||
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> | ||
<requestedExecutionLevel level="highestAvailable" uiAccess="false" /> | ||
Specifying requestedExecutionLevel element will disable file and registry virtualization. | ||
Remove this element if your application requires this virtualization for backwards | ||
compatibility. | ||
--> | ||
<requestedExecutionLevel level="asInvoker" uiAccess="false" /> | ||
</requestedPrivileges> | ||
</security> | ||
</trustInfo> | ||
|
||
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1"> | ||
<application> | ||
<!-- A list of the Windows versions that this application has been tested on and is | ||
is designed to work with. Uncomment the appropriate elements and Windows will | ||
automatically selected the most compatible environment. --> | ||
|
||
<!-- Windows Vista --> | ||
<!--<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}" />--> | ||
|
||
<!-- Windows 7 --> | ||
<!--<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}" />--> | ||
|
||
<!-- Windows 8 --> | ||
<!--<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" />--> | ||
|
||
<!-- Windows 8.1 --> | ||
<!--<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}" />--> | ||
|
||
<!-- Windows 10 --> | ||
<!--<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />--> | ||
|
||
</application> | ||
</compatibility> | ||
|
||
<!-- Indicates that the application is DPI-aware and will not be automatically scaled by Windows at higher | ||
DPIs. Windows Presentation Foundation (WPF) applications are automatically DPI-aware and do not need | ||
to opt in. Windows Forms applications targeting .NET Framework 4.6 that opt into this setting, should | ||
also set the 'EnableWindowsFormsHighDpiAutoResizing' setting to 'true' in their app.config. --> | ||
<!-- | ||
<application xmlns="urn:schemas-microsoft-com:asm.v3"> | ||
<windowsSettings> | ||
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware> | ||
</windowsSettings> | ||
</application> | ||
--> | ||
|
||
<!-- Enable themes for Windows common controls and dialogs (Windows XP and later) --> | ||
<dependency> | ||
<dependentAssembly> | ||
<assemblyIdentity | ||
type="win32" | ||
name="Microsoft.Windows.Common-Controls" | ||
version="6.0.0.0" | ||
processorArchitecture="*" | ||
publicKeyToken="6595b64144ccf1df" | ||
language="*" | ||
/> | ||
</dependentAssembly> | ||
</dependency> | ||
|
||
</assembly> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.