Skip to content

Commit

Permalink
Set up subclassing for common controls
Browse files Browse the repository at this point in the history
Subclass existing controls so we can get initial messages (such as WM_CREATE).

Port BtnLook sample from WInterop.
  • Loading branch information
JeremyKuhne committed Feb 4, 2024
1 parent 85ea173 commit 26657f4
Show file tree
Hide file tree
Showing 12 changed files with 393 additions and 34 deletions.
11 changes: 11 additions & 0 deletions src/samples/Petzold/5th/BtnLook/BtnLook.csproj
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>
177 changes: 177 additions & 0 deletions src/samples/Petzold/5th/BtnLook/Program.cs
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);
}
}
}
74 changes: 74 additions & 0 deletions src/samples/Petzold/5th/BtnLook/app.manifest
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>
4 changes: 3 additions & 1 deletion src/thirtytwo/Controls/ButtonControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public ButtonControl(
Styles buttonStyle = Styles.PushButton,
WindowStyles style = WindowStyles.Overlapped | WindowStyles.Child | WindowStyles.Visible,
ExtendedWindowStyles extendedStyle = ExtendedWindowStyles.Default,
int buttonId = default,
Window? parentWindow = default,
nint parameters = default) : base(
bounds,
Expand All @@ -23,7 +24,8 @@ public ButtonControl(
extendedStyle,
parentWindow,
s_buttonClass,
parameters)
parameters,
(HMENU)buttonId)
{
}
}
4 changes: 3 additions & 1 deletion src/thirtytwo/Controls/RichEditControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Windows;

public partial class RichEditControl : EditBase
{
private static readonly WindowClass s_richEditClass = new("RICHEDIT50W");
private static readonly WindowClass s_richEditClass;

static RichEditControl()
{
Expand All @@ -17,6 +17,8 @@ static RichEditControl()
{
Error.ThrowLastError();
}

s_richEditClass = new("RICHEDIT50W");
}

public RichEditControl(
Expand Down
57 changes: 40 additions & 17 deletions src/thirtytwo/DeviceContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,29 @@ public static bool LineTo<T>(this T context, int x, int y) where T : IHandle<HDC
return success;
}

public static bool Ellipse<T>(this T context, Rectangle rectangle) where T : IHandle<HDC> =>
context.Ellipse(rectangle.Left, rectangle.Top, rectangle.Right, rectangle.Bottom);

public static bool Ellipse<T>(this T context, int left, int top, int right, int bottom) where T : IHandle<HDC>
{
bool success = Interop.Ellipse(context.Handle, left, top, right, bottom);
GC.KeepAlive(context.Wrapper);
return success;
}

public static bool PolyBezier<T>(this T context, params Point[] points) where T : IHandle<HDC> =>
PolyBezier(context, points.AsSpan());

public static bool PolyBezier<T>(this T context, ReadOnlySpan<Point> points) where T : IHandle<HDC>
{
fixed (Point* p = points)
{
bool success = Interop.PolyBezier(context.Handle, p, (uint)points.Length);
GC.KeepAlive(context.Wrapper);
return success;
}
}

public static bool Rectangle<T>(this T context, Rectangle rectangle) where T : IHandle<HDC> =>
context.Rectangle(rectangle.Left, rectangle.Top, rectangle.Right, rectangle.Bottom);

Expand All @@ -296,34 +319,34 @@ public static bool RoundRectangle<T>(this T context, int left, int top, int righ
return success;
}

public static bool Ellipse<T>(this T context, Rectangle rectangle) where T : IHandle<HDC> =>
context.Ellipse(rectangle.Left, rectangle.Top, rectangle.Right, rectangle.Bottom);

public static bool Ellipse<T>(this T context, int left, int top, int right, int bottom) where T : IHandle<HDC>
public static bool FillRectangle<T>(this T context, Rectangle rectangle, HBRUSH hbrush) where T : IHandle<HDC>
{
bool success = Interop.Ellipse(context.Handle, left, top, right, bottom);
RECT rect = rectangle;
bool success = (BOOL)Interop.FillRect(context.Handle, &rect, hbrush);
GC.KeepAlive(context.Wrapper);
return success;
}

public static unsafe bool PolyBezier<T>(this T context, params Point[] points) where T : IHandle<HDC> =>
PolyBezier(context, points.AsSpan());
public static bool FrameRectangle<T>(this T context, Rectangle rectangle, HBRUSH brush) where T : IHandle<HDC>
{
RECT rect = rectangle;
bool success = (BOOL)Interop.FrameRect(context.Handle, &rect, brush);
GC.KeepAlive(context.Wrapper);
return success;
}

public static unsafe bool PolyBezier<T>(this T context, ReadOnlySpan<Point> points) where T : IHandle<HDC>
public static bool InvertRectangle<T>(this T context, Rectangle rectangle) where T : IHandle<HDC>
{
fixed (Point* p = points)
{
bool success = Interop.PolyBezier(context.Handle, p, (uint)points.Length);
GC.KeepAlive(context.Wrapper);
return success;
}
RECT rect = rectangle;
bool success = Interop.InvertRect(context.Handle, &rect);
GC.KeepAlive(context.Wrapper);
return success;
}

public static bool FillRectangle<T>(this T context, Rectangle rectangle, HBRUSH hbrush)
where T : IHandle<HDC>
public static bool DrawFocusRectangle<T>(this T context, Rectangle rectangle) where T : IHandle<HDC>
{
RECT rect = rectangle;
bool success = (BOOL)Interop.FillRect(context.Handle, &rect, hbrush);
bool success = Interop.DrawFocusRect(context.Handle, &rect);
GC.KeepAlive(context.Wrapper);
return success;
}
Expand Down
Loading

0 comments on commit 26657f4

Please sign in to comment.