-
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.
Port the Clover sample from WInterop
- Loading branch information
1 parent
bd5eee7
commit f01809a
Showing
13 changed files
with
327 additions
and
10 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,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> |
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,79 @@ | ||
// 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 Windows; | ||
using Windows.Win32.Foundation; | ||
using Windows.Win32.Graphics.Gdi; | ||
|
||
namespace Clover; | ||
|
||
/// <summary> | ||
/// Sample from Programming Windows, 5th Edition. | ||
/// Original (c) Charles Petzold, 1998 | ||
/// Figure 5-27, Pages 205-208. | ||
/// </summary> | ||
internal static class Program | ||
{ | ||
[STAThread] | ||
private static void Main() => Application.Run(new Clover(), "Draw a Clover"); | ||
} | ||
|
||
internal class Clover : WindowClass | ||
{ | ||
private int _cxClient, _cyClient; | ||
private HRGN _hrgnClip; | ||
private const double TWO_PI = Math.PI * 2; | ||
|
||
public Clover() : base(backgroundBrush: StockBrush.White) { } | ||
|
||
protected override LRESULT WindowProcedure(HWND window, MessageType message, WPARAM wParam, LPARAM lParam) | ||
{ | ||
switch (message) | ||
{ | ||
case MessageType.Size: | ||
{ | ||
_cxClient = lParam.LOWORD; | ||
_cyClient = lParam.HIWORD; | ||
|
||
using WaitCursorScope cursorScope = new(); | ||
|
||
_hrgnClip.Dispose(); | ||
|
||
using HRGN region0 = HRGN.FromEllipse(0, _cyClient / 3, _cxClient / 2, 2 * _cyClient / 3); | ||
using HRGN region1 = HRGN.FromEllipse(_cxClient / 2, _cyClient / 3, _cxClient, 2 * _cyClient / 3); | ||
using HRGN region2 = HRGN.FromEllipse(_cxClient / 3, 0, 2 * _cxClient / 3, _cyClient / 2); | ||
using HRGN region3 = HRGN.FromEllipse(_cxClient / 3, _cyClient / 2, 2 * _cxClient / 3, _cyClient); | ||
using HRGN region4 = HRGN.CombineRegion(region0, region1, RegionCombineMode.Or); | ||
using HRGN region5 = HRGN.CombineRegion(region2, region3, RegionCombineMode.Or); | ||
_hrgnClip = HRGN.CombineRegion(region4, region5, RegionCombineMode.Xor); | ||
|
||
return (LRESULT)0; | ||
} | ||
case MessageType.Paint: | ||
using (DeviceContext dc = window.BeginPaint()) | ||
{ | ||
dc.SetViewportOrigin(new(_cxClient / 2, _cyClient / 2)); | ||
dc.SelectClippingRegion(_hrgnClip); | ||
|
||
double fRadius = Hypotenuse(_cxClient / 2.0, _cyClient / 2.0); | ||
|
||
for (double fAngle = 0.0; fAngle < TWO_PI; fAngle += TWO_PI / 360) | ||
{ | ||
dc.MoveTo(default); | ||
dc.LineTo( | ||
(int)(fRadius * Math.Cos(fAngle) + 0.5), | ||
(int)(-fRadius * Math.Sin(fAngle) + 0.5)); | ||
} | ||
} | ||
|
||
return (LRESULT)0; | ||
case MessageType.Destroy: | ||
_hrgnClip.Dispose(); | ||
break; | ||
} | ||
|
||
return base.WindowProcedure(window, message, wParam, lParam); | ||
} | ||
|
||
private static double Hypotenuse(double x, double y) => Math.Sqrt(x * x + y * y); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// 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; | ||
|
||
/// <summary> | ||
/// Temporarily sets the cursor to the wait cursor. | ||
/// </summary> | ||
public readonly ref struct WaitCursorScope | ||
{ | ||
private readonly HCURSOR _cursor; | ||
|
||
public WaitCursorScope() | ||
{ | ||
_cursor = Interop.SetCursor(Interop.LoadCursor(default, Interop.IDC_WAIT)); | ||
_ = Interop.ShowCursor(true); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Interop.SetCursor(_cursor); | ||
_ = Interop.ShowCursor(false); | ||
} | ||
} |
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
14 changes: 14 additions & 0 deletions
14
src/thirtytwo/Win32/UI/WindowsAndMessaging/HCURSOR.SetCursorScope.cs
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,14 @@ | ||
// 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.Win32.UI.WindowsAndMessaging; | ||
|
||
public unsafe partial struct HCURSOR | ||
{ | ||
public readonly struct SetScope : IDisposable | ||
{ | ||
private readonly HCURSOR _previousCursor; | ||
public SetScope(HCURSOR cursor) => _previousCursor = Interop.SetCursor(cursor); | ||
public readonly void Dispose() => Interop.SetCursor(_previousCursor); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,11 +1,25 @@ | ||
// 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.Runtime.CompilerServices; | ||
|
||
namespace Windows.Win32.UI.WindowsAndMessaging; | ||
|
||
public unsafe partial struct HCURSOR | ||
public unsafe partial struct HCURSOR : IDisposable | ||
{ | ||
public static HCURSOR Invalid => new(-1); | ||
|
||
public static implicit operator HCURSOR(CursorId id) => Interop.LoadCursor(default, (PCWSTR)(char*)(uint)id); | ||
|
||
public SetScope SetCursorScope() => new(this); | ||
|
||
public void Dispose() | ||
{ | ||
if (!IsNull) | ||
{ | ||
Interop.DestroyCursor(this); | ||
} | ||
|
||
Unsafe.AsRef(in this) = default; | ||
} | ||
} |
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.