Skip to content

Commit

Permalink
Port AltWind sample from WInterop
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremyKuhne committed Feb 3, 2024
1 parent 9f8e0f9 commit bd5eee7
Show file tree
Hide file tree
Showing 29 changed files with 173 additions and 28 deletions.
10 changes: 10 additions & 0 deletions src/samples/Petzold/5th/AltWind/AltWind.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\..\thirtytwo\thirtytwo.csproj" />
</ItemGroup>
</Project>
77 changes: 77 additions & 0 deletions src/samples/Petzold/5th/AltWind/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// 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.Foundation;

namespace AltWind;

/// <summary>
/// Sample from Programming Windows, 5th Edition.
/// Original (c) Charles Petzold, 1998
/// Figure 5-21, Pages 171-173.
/// </summary>
internal static class Program
{
[STAThread]
private static void Main()
{
Application.Run(new AltWind(), "Alternate and Winding Fill Modes");
}
}

public class AltWind : WindowClass
{
private readonly Point[] _aptFigure =
[
new(10, 70),
new(50, 70),
new(50, 10),
new(90, 10),
new(90, 50),
new(30, 50),
new(30, 90),
new(70, 90),
new(70, 30),
new(10, 30)
];

private int _cxClient, _cyClient;

protected override LRESULT WindowProcedure(HWND window, MessageType message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case MessageType.Size:
_cxClient = lParam.LOWORD;
_cyClient = lParam.HIWORD;
return (LRESULT)0;
case MessageType.Paint:
Span<Point> apt = stackalloc Point[10];
using (DeviceContext dc = window.BeginPaint())
{
dc.SelectObject(StockBrush.Gray);
for (int i = 0; i < 10; i++)
{
apt[i].X = _cxClient * _aptFigure[i].X / 200;
apt[i].Y = _cyClient * _aptFigure[i].Y / 100;
}

dc.SetPolyFillMode(PolyFillMode.Alternate);
dc.Polygon(apt);

for (int i = 0; i < 10; i++)
{
apt[i].X += _cxClient / 2;
}
dc.SetPolyFillMode(PolyFillMode.Winding);
dc.Polygon(apt);
}

return (LRESULT)0;
}

return base.WindowProcedure(window, message, wParam, lParam);
}
}
76 changes: 49 additions & 27 deletions src/thirtytwo/DeviceContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,69 +10,69 @@ namespace Windows;
public static unsafe partial class DeviceContextExtensions
{
/// <inheritdoc cref="Interop.GetGraphicsMode(HDC)"/>
public static GRAPHICS_MODE GetGraphicsMode<T>(this T deviceContext)
public static GRAPHICS_MODE GetGraphicsMode<T>(this T context)
where T : IHandle<HDC>
{
GRAPHICS_MODE mode = (GRAPHICS_MODE)Interop.GetGraphicsMode(deviceContext.Handle);
GC.KeepAlive(deviceContext.Wrapper);
GRAPHICS_MODE mode = (GRAPHICS_MODE)Interop.GetGraphicsMode(context.Handle);
GC.KeepAlive(context.Wrapper);
return mode;
}

/// <inheritdoc cref="Interop.SetGraphicsMode(HDC, GRAPHICS_MODE)"/>
public static GRAPHICS_MODE SetGraphicsMode<T>(this T deviceContext, GRAPHICS_MODE mode)
public static GRAPHICS_MODE SetGraphicsMode<T>(this T context, GRAPHICS_MODE mode)
where T : IHandle<HDC>
{
mode = (GRAPHICS_MODE)Interop.SetGraphicsMode(deviceContext.Handle, mode);
GC.KeepAlive(deviceContext.Wrapper);
mode = (GRAPHICS_MODE)Interop.SetGraphicsMode(context.Handle, mode);
GC.KeepAlive(context.Wrapper);
return mode;
}

/// <inheritdoc cref="Interop.GetWorldTransform(HDC, XFORM*)"/>
public static unsafe bool GetWorldTransform<T>(this T deviceContext, ref Matrix3x2 transform)
public static unsafe bool GetWorldTransform<T>(this T context, ref Matrix3x2 transform)
where T : IHandle<HDC>
{
fixed (Matrix3x2* t = &transform)
{
bool result = Interop.GetWorldTransform(deviceContext.Handle, (XFORM*)t);
GC.KeepAlive(deviceContext.Wrapper);
bool result = Interop.GetWorldTransform(context.Handle, (XFORM*)t);
GC.KeepAlive(context.Wrapper);
return result;
}
}

/// <inheritdoc cref="Interop.SetWorldTransform(HDC, XFORM*)"/>
public static unsafe bool SetWorldTransform<T>(this T deviceContext, ref Matrix3x2 transform)
public static unsafe bool SetWorldTransform<T>(this T context, ref Matrix3x2 transform)
where T : IHandle<HDC>
{
fixed (Matrix3x2* t = &transform)
{
bool result = Interop.SetWorldTransform(deviceContext.Handle, (XFORM*)t);
GC.KeepAlive(deviceContext.Wrapper);
bool result = Interop.SetWorldTransform(context.Handle, (XFORM*)t);
GC.KeepAlive(context.Wrapper);
return result;
}
}

/// <inheritdoc cref="Interop.GetDeviceCaps(HDC, GET_DEVICE_CAPS_INDEX)"/>
public static int GetDeviceCaps<T>(this T deviceContext, GET_DEVICE_CAPS_INDEX index)
public static int GetDeviceCaps<T>(this T context, GET_DEVICE_CAPS_INDEX index)
where T : IHandle<HDC>
{
int result = Interop.GetDeviceCaps(deviceContext.Handle, index);
GC.KeepAlive(deviceContext.Wrapper);
int result = Interop.GetDeviceCaps(context.Handle, index);
GC.KeepAlive(context.Wrapper);
return result;
}

/// <summary>
/// Converts the requested point size to height based on the DPI of the given device context.
/// </summary>
public static int FontPointSizeToHeight<T>(this T deviceContext, int pointSize)
public static int FontPointSizeToHeight<T>(this T context, int pointSize)
where T : IHandle<HDC>
{
Application.EnsureDpiAwareness();
int result = Interop.MulDiv(
pointSize,
Interop.GetDeviceCaps(deviceContext.Handle, GET_DEVICE_CAPS_INDEX.LOGPIXELSY),
Interop.GetDeviceCaps(context.Handle, GET_DEVICE_CAPS_INDEX.LOGPIXELSY),
72);

GC.KeepAlive(deviceContext.Wrapper);
GC.KeepAlive(context.Wrapper);
return result;
}

Expand All @@ -89,6 +89,28 @@ public static ObjectScope<T> SelectObject<T>(this T context, HGDIOBJ @object)
return type == OBJ_TYPE.OBJ_REGION ? default : new(handle, context);
}

public static PolyFillMode SetPolyFillMode<T>(this T context, PolyFillMode mode)
where T : IHandle<HDC>
{
PolyFillMode result = (PolyFillMode)Interop.SetPolyFillMode(context.Handle, (CREATE_POLYGON_RGN_MODE)mode);
GC.KeepAlive(context.Wrapper);
return result;
}

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

Check warning on line 101 in src/thirtytwo/DeviceContextExtensions.cs

View workflow job for this annotation

GitHub Actions / build

Check warning on line 101 in src/thirtytwo/DeviceContextExtensions.cs

View workflow job for this annotation

GitHub Actions / build

Check warning on line 101 in src/thirtytwo/DeviceContextExtensions.cs

View workflow job for this annotation

GitHub Actions / build

Check warning on line 101 in src/thirtytwo/DeviceContextExtensions.cs

View workflow job for this annotation

GitHub Actions / build


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

public static unsafe (int Height, uint LengthDrawn, Rectangle Bounds) DrawText<T>(
this T context,
ReadOnlySpan<char> text,
Expand Down Expand Up @@ -152,46 +174,46 @@ private static (int Height, uint LengthDrawn, Rectangle Bounds) DrawTextHelper<T
}

public static void DrawIcon<TDeviceContext, TIcon>(
this TDeviceContext deviceContext,
this TDeviceContext context,
TIcon icon,
Point location,
Size size = default,
DI_FLAGS flags = DI_FLAGS.DI_NORMAL)
where TDeviceContext : IHandle<HDC>
where TIcon : IHandle<HICON>
{
if (!Interop.DrawIconEx(deviceContext.Handle, location.X, location.Y, icon.Handle, size.Width, size.Height, 0, default, flags))
if (!Interop.DrawIconEx(context.Handle, location.X, location.Y, icon.Handle, size.Width, size.Height, 0, default, flags))
{
Error.ThrowLastError();
}

GC.KeepAlive(deviceContext.Wrapper);
GC.KeepAlive(context.Wrapper);
GC.KeepAlive(icon.Wrapper);
}

public static DeviceContext CreateCompatibleDeviceContext<TDeviceContext>(this TDeviceContext deviceContext)
public static DeviceContext CreateCompatibleDeviceContext<TDeviceContext>(this TDeviceContext context)
where TDeviceContext : IHandle<HDC>
{
HDC hdc = Interop.CreateCompatibleDC(deviceContext.Handle);
HDC hdc = Interop.CreateCompatibleDC(context.Handle);
if (hdc.IsNull)
{
Error.ThrowLastError();
}

GC.KeepAlive(deviceContext.Wrapper);
GC.KeepAlive(context.Wrapper);
return DeviceContext.Create(hdc, ownsHandle: true);
}

public static Bitmap CreateCompatibleBitmap<TDeviceContext>(this TDeviceContext deviceContext, Size size)
public static Bitmap CreateCompatibleBitmap<TDeviceContext>(this TDeviceContext context, Size size)
where TDeviceContext : IHandle<HDC>
{
HBITMAP hbitmap = Interop.CreateCompatibleBitmap(deviceContext.Handle, size.Width, size.Height);
HBITMAP hbitmap = Interop.CreateCompatibleBitmap(context.Handle, size.Width, size.Height);
if (hbitmap.IsNull)
{
Error.ThrowLastError();
}

GC.KeepAlive(deviceContext.Wrapper);
GC.KeepAlive(context.Wrapper);
return Bitmap.Create(hbitmap, ownsHandle: true);
}
}
2 changes: 2 additions & 0 deletions src/thirtytwo/NativeMethods.txt
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,5 @@ WIN32_ERROR
WINDOWPOS
WM_*
XFORMCOORDS
SetPolyFillMode
Polygon
6 changes: 6 additions & 0 deletions src/thirtytwo/Win32/Graphics/Gdi/HGDIOBJ.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,10 @@ namespace Windows.Win32.Graphics.Gdi;
public unsafe partial struct HGDIOBJ
{
public OBJ_TYPE GetObjectType() => (OBJ_TYPE)Interop.GetObjectType(this);

public static implicit operator HGDIOBJ(StockFont font) => new(Interop.GetStockObject((GET_STOCK_OBJECT_FLAGS)font));
public static implicit operator HGDIOBJ(StockBrush brush) => new(Interop.GetStockObject((GET_STOCK_OBJECT_FLAGS)brush));
public static implicit operator HGDIOBJ(SystemColor color) => new(Interop.GetStockObject((GET_STOCK_OBJECT_FLAGS)color));
public static implicit operator HGDIOBJ(StockPen pen) => new(Interop.GetStockObject((GET_STOCK_OBJECT_FLAGS)pen));
public static implicit operator HGDIOBJ(GET_STOCK_OBJECT_FLAGS stockObject) => new(Interop.GetStockObject(stockObject));
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
// Copyright (c) Jeremy W. Kuhne. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Windows;
namespace Windows;

public enum PolyFillMode : uint
{
Alternate = CREATE_POLYGON_RGN_MODE.ALTERNATE,
Winding = CREATE_POLYGON_RGN_MODE.WINDING
}
File renamed without changes.
File renamed without changes.
12 changes: 12 additions & 0 deletions src/thirtytwo/WrapperEnums/StockPen.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) Jeremy W. Kuhne. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Windows;

public enum StockPen : uint
{
White = GET_STOCK_OBJECT_FLAGS.WHITE_PEN,
Black = GET_STOCK_OBJECT_FLAGS.BLACK_PEN,
Null = GET_STOCK_OBJECT_FLAGS.NULL_PEN,
DeviceContext = GET_STOCK_OBJECT_FLAGS.DC_PEN
}
File renamed without changes.
File renamed without changes.
10 changes: 10 additions & 0 deletions thirtytwo.sln
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Transparency", "src\samples
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ClipboardViewer", "src\samples\ClipboardViewer\ClipboardViewer.csproj", "{3D0531EE-7ED0-42EC-8A4C-3D69F3CBF0F4}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Petzold", "Petzold", "{13110246-EBE1-441B-B721-B0614D62B13B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AltWind", "src\samples\Petzold\5th\AltWind\AltWind.csproj", "{1DFDC4A1-DF15-402F-A6AB-5C90A2B71C48}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Expand Down Expand Up @@ -69,6 +73,10 @@ Global
{3D0531EE-7ED0-42EC-8A4C-3D69F3CBF0F4}.Debug|x64.Build.0 = Debug|x64
{3D0531EE-7ED0-42EC-8A4C-3D69F3CBF0F4}.Release|x64.ActiveCfg = Release|x64
{3D0531EE-7ED0-42EC-8A4C-3D69F3CBF0F4}.Release|x64.Build.0 = Release|x64
{1DFDC4A1-DF15-402F-A6AB-5C90A2B71C48}.Debug|x64.ActiveCfg = Debug|x64
{1DFDC4A1-DF15-402F-A6AB-5C90A2B71C48}.Debug|x64.Build.0 = Debug|x64
{1DFDC4A1-DF15-402F-A6AB-5C90A2B71C48}.Release|x64.ActiveCfg = Release|x64
{1DFDC4A1-DF15-402F-A6AB-5C90A2B71C48}.Release|x64.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -80,6 +88,8 @@ Global
{01DE5FF9-428C-49BA-8853-6DFCB62085B9} = {C059056C-D771-4CF5-A93F-AA7140FF7827}
{4E2E6DCA-8E84-497B-937D-8F776B98E453} = {C059056C-D771-4CF5-A93F-AA7140FF7827}
{3D0531EE-7ED0-42EC-8A4C-3D69F3CBF0F4} = {C059056C-D771-4CF5-A93F-AA7140FF7827}
{13110246-EBE1-441B-B721-B0614D62B13B} = {C059056C-D771-4CF5-A93F-AA7140FF7827}
{1DFDC4A1-DF15-402F-A6AB-5C90A2B71C48} = {13110246-EBE1-441B-B721-B0614D62B13B}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3761BFC9-DBEF-4186-BB8B-BC0D84ED9AE5}
Expand Down

0 comments on commit bd5eee7

Please sign in to comment.