Skip to content

Commit e26b54c

Browse files
committed
Use CsWin32
1 parent d00b356 commit e26b54c

File tree

3 files changed

+41
-264
lines changed

3 files changed

+41
-264
lines changed

components/Helpers/src/CommunityToolkit.WinUI.Helpers.csproj

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
</PropertyGroup>
1212

1313
<ItemGroup>
14-
<ProjectReference Include="$(ToolkitExtensionsSourceProject)"/>
14+
<ProjectReference Include="$(ToolkitExtensionsSourceProject)" />
1515
</ItemGroup>
1616

1717
<ItemGroup>
@@ -20,6 +20,13 @@
2020
<PackagePath>\</PackagePath>
2121
</None>
2222
</ItemGroup>
23+
24+
<ItemGroup>
25+
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.3.106">
26+
<PrivateAssets>all</PrivateAssets>
27+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
28+
</PackageReference>
29+
</ItemGroup>
2330

2431
<!-- Sets this up as a toolkit component's source project -->
2532
<Import Project="$(ToolingDirectory)\ToolkitComponent.SourceProject.props" />
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
RegisterClassEx
2+
DefWindowProc
3+
CreateWindowEx
4+
WM_SETTINGCHANGE

components/Helpers/src/ThemeListenerHelperWindow.cs

Lines changed: 29 additions & 263 deletions
Original file line numberDiff line numberDiff line change
@@ -9,256 +9,11 @@
99
using System.Runtime.InteropServices;
1010
using System.Text;
1111
using System.Threading.Tasks;
12+
using Windows.Win32;
1213

1314
namespace CommunityToolkit.WinUI.HelpersRns;
1415

1516
#if WINAPPSDK
16-
internal static class WinAPI
17-
{
18-
[Flags]
19-
public enum WindowStyles : uint
20-
{
21-
/// <summary>The window has a thin-line border.</summary>
22-
WS_BORDER = 0x800000,
23-
24-
/// <summary>The window has a title bar (includes the WS_BORDER style).</summary>
25-
WS_CAPTION = 0xc00000,
26-
27-
/// <summary>The window is a child window. A window with this style cannot have a menu bar. This style cannot be used with the WS_POPUP style.</summary>
28-
WS_CHILD = 0x40000000,
29-
30-
/// <summary>Excludes the area occupied by child windows when drawing occurs within the parent window. This style is used when creating the parent window.</summary>
31-
WS_CLIPCHILDREN = 0x2000000,
32-
33-
/// <summary>
34-
/// Clips child windows relative to each other; that is, when a particular child window receives a WM_PAINT message, the WS_CLIPSIBLINGS style clips all other overlapping child windows out of the region of the child window to be updated.
35-
/// If WS_CLIPSIBLINGS is not specified and child windows overlap, it is possible, when drawing within the client area of a child window, to draw within the client area of a neighboring child window.
36-
/// </summary>
37-
WS_CLIPSIBLINGS = 0x4000000,
38-
39-
/// <summary>The window is initially disabled. A disabled window cannot receive input from the user. To change this after a window has been created, use the EnableWindow function.</summary>
40-
WS_DISABLED = 0x8000000,
41-
42-
/// <summary>The window has a border of a style typically used with dialog boxes. A window with this style cannot have a title bar.</summary>
43-
WS_DLGFRAME = 0x400000,
44-
45-
/// <summary>
46-
/// The window is the first control of a group of controls. The group consists of this first control and all controls defined after it, up to the next control with the WS_GROUP style.
47-
/// The first control in each group usually has the WS_TABSTOP style so that the user can move from group to group. The user can subsequently change the keyboard focus from one control in the group to the next control in the group by using the direction keys.
48-
/// You can turn this style on and off to change dialog box navigation. To change this style after a window has been created, use the SetWindowLong function.
49-
/// </summary>
50-
WS_GROUP = 0x20000,
51-
52-
/// <summary>The window has a horizontal scroll bar.</summary>
53-
WS_HSCROLL = 0x100000,
54-
55-
/// <summary>The window is initially maximized.</summary>
56-
WS_MAXIMIZE = 0x1000000,
57-
58-
/// <summary>The window has a maximize button. Cannot be combined with the WS_EX_CONTEXTHELP style. The WS_SYSMENU style must also be specified.</summary>
59-
WS_MAXIMIZEBOX = 0x10000,
60-
61-
/// <summary>The window is initially minimized.</summary>
62-
WS_MINIMIZE = 0x20000000,
63-
64-
/// <summary>The window has a minimize button. Cannot be combined with the WS_EX_CONTEXTHELP style. The WS_SYSMENU style must also be specified.</summary>
65-
WS_MINIMIZEBOX = 0x20000,
66-
67-
/// <summary>The window is an overlapped window. An overlapped window has a title bar and a border.</summary>
68-
WS_OVERLAPPED = 0x0,
69-
70-
/// <summary>The window is an overlapped window.</summary>
71-
WS_OVERLAPPEDWINDOW = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_SIZEFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,
72-
73-
/// <summary>The window is a pop-up window. This style cannot be used with the WS_CHILD style.</summary>
74-
WS_POPUP = 0x80000000u,
75-
76-
/// <summary>The window is a pop-up window. The WS_CAPTION and WS_POPUPWINDOW styles must be combined to make the window menu visible.</summary>
77-
WS_POPUPWINDOW = WS_POPUP | WS_BORDER | WS_SYSMENU,
78-
79-
/// <summary>The window has a sizing border.</summary>
80-
WS_SIZEFRAME = 0x40000,
81-
82-
/// <summary>The window has a window menu on its title bar. The WS_CAPTION style must also be specified.</summary>
83-
WS_SYSMENU = 0x80000,
84-
85-
/// <summary>
86-
/// The window is a control that can receive the keyboard focus when the user presses the TAB key.
87-
/// Pressing the TAB key changes the keyboard focus to the next control with the WS_TABSTOP style.
88-
/// You can turn this style on and off to change dialog box navigation. To change this style after a window has been created, use the SetWindowLong function.
89-
/// For user-created windows and modeless dialogs to work with tab stops, alter the message loop to call the IsDialogMessage function.
90-
/// </summary>
91-
WS_TABSTOP = 0x10000,
92-
93-
/// <summary>The window is initially visible. This style can be turned on and off by using the ShowWindow or SetWindowPos function.</summary>
94-
WS_VISIBLE = 0x10000000,
95-
96-
/// <summary>The window has a vertical scroll bar.</summary>
97-
WS_VSCROLL = 0x200000
98-
}
99-
100-
[Flags]
101-
public enum WindowStylesEx : uint
102-
{
103-
/// <summary>
104-
/// Specifies that a window created with this style accepts drag-drop files.
105-
/// </summary>
106-
WS_EX_ACCEPTFILES = 0x00000010,
107-
/// <summary>
108-
/// Forces a top-level window onto the taskbar when the window is visible.
109-
/// </summary>
110-
WS_EX_APPWINDOW = 0x00040000,
111-
/// <summary>
112-
/// Specifies that a window has a border with a sunken edge.
113-
/// </summary>
114-
WS_EX_CLIENTEDGE = 0x00000200,
115-
/// <summary>
116-
/// Windows XP: Paints all descendants of a window in bottom-to-top painting order using double-buffering. For more information, see Remarks. This cannot be used if the window has a class style of either CS_OWNDC or CS_CLASSDC.
117-
/// </summary>
118-
WS_EX_COMPOSITED = 0x02000000,
119-
/// <summary>
120-
/// Includes a question mark in the title bar of the window. When the user clicks the question mark, the cursor changes to a question mark with a pointer. If the user then clicks a child window, the child receives a WM_HELP message. The child window should pass the message to the parent window procedure, which should call the WinHelp function using the HELP_WM_HELP command. The Help application displays a pop-up window that typically contains help for the child window.
121-
/// WS_EX_CONTEXTHELP cannot be used with the WS_MAXIMIZEBOX or WS_MINIMIZEBOX styles.
122-
/// </summary>
123-
WS_EX_CONTEXTHELP = 0x00000400,
124-
/// <summary>
125-
/// The window itself contains child windows that should take part in dialog box navigation. If this style is specified, the dialog manager recurses into children of this window when performing navigation operations such as handling the TAB key, an arrow key, or a keyboard mnemonic.
126-
/// </summary>
127-
WS_EX_CONTROLPARENT = 0x00010000,
128-
/// <summary>
129-
/// Creates a window that has a double border; the window can, optionally, be created with a title bar by specifying the WS_CAPTION style in the dwStyle parameter.
130-
/// </summary>
131-
WS_EX_DLGMODALFRAME = 0x00000001,
132-
/// <summary>
133-
/// Windows 2000/XP: Creates a layered window. Note that this cannot be used for child windows. Also, this cannot be used if the window has a class style of either CS_OWNDC or CS_CLASSDC.
134-
/// </summary>
135-
WS_EX_LAYERED = 0x00080000,
136-
/// <summary>
137-
/// Arabic and Hebrew versions of Windows 98/Me, Windows 2000/XP: Creates a window whose horizontal origin is on the right edge. Increasing horizontal values advance to the left.
138-
/// </summary>
139-
WS_EX_LAYOUTRTL = 0x00400000,
140-
/// <summary>
141-
/// Creates a window that has generic left-aligned properties. This is the default.
142-
/// </summary>
143-
WS_EX_LEFT = 0x00000000,
144-
/// <summary>
145-
/// If the shell language is Hebrew, Arabic, or another language that supports reading order alignment, the vertical scroll bar (if present) is to the left of the client area. For other languages, the style is ignored.
146-
/// </summary>
147-
WS_EX_LEFTSCROLLBAR = 0x00004000,
148-
/// <summary>
149-
/// The window text is displayed using left-to-right reading-order properties. This is the default.
150-
/// </summary>
151-
WS_EX_LTRREADING = 0x00000000,
152-
/// <summary>
153-
/// Creates a multiple-document interface (MDI) child window.
154-
/// </summary>
155-
WS_EX_MDICHILD = 0x00000040,
156-
/// <summary>
157-
/// Windows 2000/XP: A top-level window created with this style does not become the foreground window when the user clicks it. The system does not bring this window to the foreground when the user minimizes or closes the foreground window.
158-
/// To activate the window, use the SetActiveWindow or SetForegroundWindow function.
159-
/// The window does not appear on the taskbar by default. To force the window to appear on the taskbar, use the WS_EX_APPWINDOW style.
160-
/// </summary>
161-
WS_EX_NOACTIVATE = 0x08000000,
162-
/// <summary>
163-
/// Windows 2000/XP: A window created with this style does not pass its window layout to its child windows.
164-
/// </summary>
165-
WS_EX_NOINHERITLAYOUT = 0x00100000,
166-
/// <summary>
167-
/// Specifies that a child window created with this style does not send the WM_PARENTNOTIFY message to its parent window when it is created or destroyed.
168-
/// </summary>
169-
WS_EX_NOPARENTNOTIFY = 0x00000004,
170-
/// <summary>
171-
/// Combines the WS_EX_CLIENTEDGE and WS_EX_WINDOWEDGE styles.
172-
/// </summary>
173-
WS_EX_OVERLAPPEDWINDOW = WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE,
174-
/// <summary>
175-
/// Combines the WS_EX_WINDOWEDGE, WS_EX_TOOLWINDOW, and WS_EX_TOPMOST styles.
176-
/// </summary>
177-
WS_EX_PALETTEWINDOW = WS_EX_WINDOWEDGE | WS_EX_TOOLWINDOW | WS_EX_TOPMOST,
178-
/// <summary>
179-
/// The window has generic "right-aligned" properties. This depends on the window class. This style has an effect only if the shell language is Hebrew, Arabic, or another language that supports reading-order alignment; otherwise, the style is ignored.
180-
/// Using the WS_EX_RIGHT style for static or edit controls has the same effect as using the SS_RIGHT or ES_RIGHT style, respectively. Using this style with button controls has the same effect as using BS_RIGHT and BS_RIGHTBUTTON styles.
181-
/// </summary>
182-
WS_EX_RIGHT = 0x00001000,
183-
/// <summary>
184-
/// Vertical scroll bar (if present) is to the right of the client area. This is the default.
185-
/// </summary>
186-
WS_EX_RIGHTSCROLLBAR = 0x00000000,
187-
/// <summary>
188-
/// If the shell language is Hebrew, Arabic, or another language that supports reading-order alignment, the window text is displayed using right-to-left reading-order properties. For other languages, the style is ignored.
189-
/// </summary>
190-
WS_EX_RTLREADING = 0x00002000,
191-
/// <summary>
192-
/// Creates a window with a three-dimensional border style intended to be used for items that do not accept user input.
193-
/// </summary>
194-
WS_EX_STATICEDGE = 0x00020000,
195-
/// <summary>
196-
/// Creates a tool window; that is, a window intended to be used as a floating toolbar. A tool window has a title bar that is shorter than a normal title bar, and the window title is drawn using a smaller font. A tool window does not appear in the taskbar or in the dialog that appears when the user presses ALT+TAB. If a tool window has a system menu, its icon is not displayed on the title bar. However, you can display the system menu by right-clicking or by typing ALT+SPACE.
197-
/// </summary>
198-
WS_EX_TOOLWINDOW = 0x00000080,
199-
/// <summary>
200-
/// Specifies that a window created with this style should be placed above all non-topmost windows and should stay above them, even when the window is deactivated. To add or remove this style, use the SetWindowPos function.
201-
/// </summary>
202-
WS_EX_TOPMOST = 0x00000008,
203-
/// <summary>
204-
/// Specifies that a window created with this style should not be painted until siblings beneath the window (that were created by the same thread) have been painted. The window appears transparent because the bits of underlying sibling windows have already been painted.
205-
/// To achieve transparency without these restrictions, use the SetWindowRgn function.
206-
/// </summary>
207-
WS_EX_TRANSPARENT = 0x00000020,
208-
/// <summary>
209-
/// Specifies that a window has a border with a raised edge.
210-
/// </summary>
211-
WS_EX_WINDOWEDGE = 0x00000100
212-
}
213-
214-
[DllImport("user32.dll", SetLastError = true, EntryPoint = "CreateWindowEx")]
215-
public static extern IntPtr CreateWindowEx(
216-
WindowStylesEx dwExStyle,
217-
string lpClassName,
218-
string lpWindowName,
219-
WindowStyles dwStyle,
220-
int x,
221-
int y,
222-
int nWidth,
223-
int nHeight,
224-
IntPtr hWndParent,
225-
IntPtr hMenu,
226-
IntPtr hInstance,
227-
IntPtr lpParam);
228-
229-
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
230-
public struct WNDCLASSEX
231-
{
232-
[MarshalAs(UnmanagedType.U4)]
233-
public int cbSize;
234-
[MarshalAs(UnmanagedType.U4)]
235-
public int style;
236-
public IntPtr lpfnWndProc; // not WndProc -- careful
237-
public int cbClsExtra;
238-
public int cbWndExtra;
239-
public IntPtr hInstance;
240-
public IntPtr hIcon;
241-
public IntPtr hCursor;
242-
public IntPtr hbrBackground;
243-
[MarshalAs(UnmanagedType.LPStr)]
244-
public string lpszMenuName;
245-
[MarshalAs(UnmanagedType.LPStr)]
246-
public string lpszClassName;
247-
public IntPtr hIconSm;
248-
}
249-
250-
[DllImport("user32.dll")]
251-
public static extern short RegisterClassEx([In] ref WNDCLASSEX lpwcx);
252-
253-
254-
public enum WM : uint
255-
{
256-
SettingChange = 0x001A
257-
}
258-
259-
[DllImport("user32.dll")]
260-
public static extern IntPtr DefWindowProc(IntPtr hWnd, WM uMsg, IntPtr wParam, IntPtr lParam);
261-
}
26217

26318
internal class ThemeListenerHelperWindow
26419
{
@@ -268,47 +23,58 @@ internal class ThemeListenerHelperWindow
26823
private static string s_className = "ThemeListenerHelperWindow";
26924
private IntPtr m_hwnd;
27025
public event ThemeChangedHandler? ThemeChanged;
271-
delegate IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
26+
delegate Windows.Win32.Foundation.LRESULT WndProc(Windows.Win32.Foundation.HWND hWnd, uint msg, Windows.Win32.Foundation.WPARAM wParam, Windows.Win32.Foundation.LPARAM lParam);
27227
private WndProc m_wnd_proc_delegate;
27328
private void registerClass()
27429
{
275-
var wcx = new WinAPI.WNDCLASSEX();
276-
wcx.cbSize = Marshal.SizeOf(wcx);
277-
wcx.lpszClassName = s_className;
278-
wcx.lpfnWndProc = Marshal.GetFunctionPointerForDelegate(m_wnd_proc_delegate);
279-
if(WinAPI.RegisterClassEx(ref wcx) == 0)
30+
unsafe
28031
{
281-
Debug.WriteLine(new Win32Exception(Marshal.GetLastWin32Error()).Message);
32+
var wcx = new Windows.Win32.UI.WindowsAndMessaging.WNDCLASSEXW();
33+
wcx.cbSize = (uint)Marshal.SizeOf(wcx);
34+
fixed (char* pClassName = s_className)
35+
{
36+
wcx.lpszClassName = new Windows.Win32.Foundation.PCWSTR(pClassName);
37+
}
38+
wcx.lpfnWndProc = wndProc;
39+
if (PInvoke.RegisterClassEx(in wcx) == 0)
40+
{
41+
Debug.WriteLine(new Win32Exception(Marshal.GetLastWin32Error()).Message);
42+
}
28243
}
44+
28345
}
28446

285-
static IntPtr wndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
47+
static Windows.Win32.Foundation.LRESULT wndProc(Windows.Win32.Foundation.HWND hWnd, uint msg, Windows.Win32.Foundation.WPARAM wParam, Windows.Win32.Foundation.LPARAM lParam)
28648
{
28749
switch (msg)
28850
{
289-
case (uint)WinAPI.WM.SettingChange:
290-
var value = Registry.GetValue("""HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize""", "AppsUseLightTheme", true);
51+
case PInvoke.WM_SETTINGCHANGE:
52+
var value = Registry.GetValue(
53+
"""HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize""",
54+
"AppsUseLightTheme",
55+
true
56+
);
29157
if (value != null)
29258
{
29359
Instance.ThemeChanged?.Invoke((int)value == 1 ? ApplicationTheme.Light : ApplicationTheme.Dark);
29460
}
29561
break;
29662
}
297-
return WinAPI.DefWindowProc(hWnd, (WinAPI.WM)msg, wParam, lParam);
63+
return PInvoke.DefWindowProc(hWnd, msg, wParam, lParam);
29864
}
29965

300-
private static IntPtr createWindow()
66+
unsafe private static IntPtr createWindow()
30167
{
302-
var hwnd = WinAPI.CreateWindowEx(
68+
var hwnd = PInvoke.CreateWindowEx(
30369
0,
30470
s_className,
30571
"",
30672
0,
30773
0, 0, 0, 0,
308-
IntPtr.Zero,
309-
IntPtr.Zero,
310-
IntPtr.Zero,
311-
IntPtr.Zero);
74+
new Windows.Win32.Foundation.HWND(),
75+
null,
76+
null,
77+
null);
31278
if (hwnd == IntPtr.Zero)
31379
{
31480
Debug.WriteLine(new Win32Exception(Marshal.GetLastWin32Error()).Message);

0 commit comments

Comments
 (0)