Add some WinUI work

This commit is contained in:
Dan Clark
2024-11-17 21:25:27 +00:00
parent b5bf17821c
commit 796ef41e3f
25 changed files with 426 additions and 159 deletions
+42 -37
View File
@@ -1,7 +1,7 @@
using CommunityToolkit.Mvvm.Messaging;
using Toolkit.Foundation;
using Windows.Win32;
using Windows.Win32.Foundation;
using Windows.Win32.Graphics.Gdi;
using Windows.Win32.UI.WindowsAndMessaging;
namespace Toolkit.Windows;
@@ -10,56 +10,61 @@ public class WndProc(IMessenger messenger) :
IWndProc
{
private WNDPROC? handler;
private bool isDisposed;
~WndProc()
{
Dispose(false);
}
public IntPtr Handle { get; private set; }
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
public void Initialize() => InitializeWndProc();
protected virtual void Dispose(bool disposing)
{
if (isDisposed)
{
return;
}
isDisposed = true;
PInvoke.DestroyWindow((HWND)Handle);
}
private LRESULT HandleWndProc(HWND hWnd, uint msg,
WPARAM wParam, LPARAM lParam)
{
messenger.Send(new WndProcEventArgs(msg,
(uint)wParam.Value,
(uint)lParam.Value));
return PInvoke.DefWindowProc(hWnd, msg, wParam, lParam);
}
private unsafe void InitializeWndProc()
{
string windowName = Guid.NewGuid().ToString();
handler = Wndproc;
string windowId = $"WndProc_Handler_{Guid.NewGuid()}";
handler = HandleWndProc;
WNDCLASSW wndProcWindow;
wndProcWindow.style = 0;
wndProcWindow.lpfnWndProc = handler;
wndProcWindow.cbClsExtra = 0;
wndProcWindow.cbWndExtra = 0;
wndProcWindow.hInstance = new HINSTANCE();
wndProcWindow.hIcon = new HICON();
wndProcWindow.hCursor = new HCURSOR();
wndProcWindow.hbrBackground = new HBRUSH();
fixed (char* menuName = "")
fixed (char* className = windowId)
{
wndProcWindow.lpszMenuName = new PCWSTR(menuName);
WNDCLASSW wndCLass = new()
{
lpfnWndProc = handler,
lpszClassName = className,
};
_ = PInvoke.RegisterClass(wndCLass);
}
fixed (char* className = windowName)
{
wndProcWindow.lpszClassName = new PCWSTR(className);
}
PInvoke.RegisterClass(wndProcWindow);
Handle = PInvoke.CreateWindowEx(0, wndProcWindow.lpszClassName,
new PCWSTR(), 0, 0, 0, 0, 0, new HWND(),
new HMENU(),
new HINSTANCE());
}
private LRESULT Wndproc(HWND param0, uint param1, WPARAM param2, LPARAM param3)
{
messenger.Send(new WndProcEventArgs(param1, (uint)param2.Value, (uint)param3.Value));
return PInvoke.DefWindowProc(param0, param1, param2, param3);
}
public void Initialize()
{
InitializeWndProc();
Handle = PInvoke.CreateWindowEx(0, windowId, windowId, 0, 0, 0, 0, 0,
new HWND(IntPtr.Zero), null, null, null);
}
}