1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
|
public partial class DesktopWindow { private bool m_bIsClosed = false; private DesktopWindowXamlSource m_source; private IDesktopWindowXamlSourceNative m_native;
private readonly HWND m_hwnd; private readonly WNDCLASSEXW m_wndClassEx;
public DesktopWindow() { m_wndClassEx = RegisterDesktopWindowClass(WNDPROC); m_hwnd = CreateDesktopWindow(); }
public nint Hwnd => m_hwnd;
public CoreDispatcher Dispatcher { get; private set; }
public UIElement Content { get => WindowXamlSource.Content; set => WindowXamlSource.Content = value; }
[SupportedOSPlatform("Windows10.0.18362.0")] public XamlRoot XamlRoot { get => WindowXamlSource.Content.XamlRoot; set => WindowXamlSource.Content.XamlRoot = value; }
public unsafe string Title { get { int windowTextLength = PInvoke.GetWindowTextLength(m_hwnd); char* windowText = stackalloc char[windowTextLength + 1]; _ = PInvoke.GetWindowText(m_hwnd, windowText, windowTextLength + 1); return new string(windowText); } set => _ = PInvoke.SetWindowText(m_hwnd, value); }
public DesktopWindowXamlSource WindowXamlSource { get => m_source; private init { if (m_source != value) { m_source = value; if (value != null) { Dispatcher = CoreWindow.GetForCurrentThread().Dispatcher; m_native = value.As<IDesktopWindowXamlSourceNative>(); m_native.AttachToWindow(m_hwnd); ResizeWindowToDesktopWindowXamlSourceWindowDimensions(); } else { m_native = null; } } } }
public event TypedEventHandler<DesktopWindow, object> Closed;
public void Show() => _ = PInvoke.ShowWindow(m_hwnd, SHOW_WINDOW_CMD.SW_NORMAL);
public unsafe void SetIcon(string iconPath) { fixed (char* ptr = iconPath) { HANDLE icon = PInvoke.LoadImage(new HINSTANCE(), ptr, GDI_IMAGE_TYPE.IMAGE_ICON, 0, 0, IMAGE_FLAGS.LR_LOADFROMFILE); _ = PInvoke.SendMessage(m_hwnd, PInvoke.WM_SETICON, PInvoke.ICON_BIG, new LPARAM((nint)icon.Value)); } }
private LRESULT WNDPROC(HWND hWnd, uint message, WPARAM wParam, LPARAM lParam) { switch (message) { case PInvoke.WM_PAINT: HDC hdc = PInvoke.BeginPaint(hWnd, out PAINTSTRUCT ps); _ = PInvoke.GetClientRect(hWnd, out RECT rect); _ = PInvoke.FillRect(hdc, rect, new DefaultSafeHandle(PInvoke.GetStockObject(GET_STOCK_OBJECT_FLAGS.WHITE_BRUSH))); _ = PInvoke.EndPaint(hWnd, ps); return new LRESULT(); case PInvoke.WM_CLOSE when m_bIsClosed: goto default; case PInvoke.WM_CLOSE: m_bIsClosed = true; Closed?.Invoke(this, null); goto default; case PInvoke.WM_SIZE: ResizeWindowToDesktopWindowXamlSourceWindowDimensions(); return new LRESULT(); case PInvoke.WM_CREATE: return new LRESULT(); case PInvoke.WM_DESTROY: PInvoke.PostQuitMessage(0); return new LRESULT(); default: return PInvoke.DefWindowProc(hWnd, message, wParam, lParam); } }
private void ResizeWindowToDesktopWindowXamlSourceWindowDimensions() { if (m_bIsClosed) return; _ = PInvoke.GetClientRect(m_hwnd, out RECT rect); _ = PInvoke.SetWindowPos( m_native.WindowHandle(), new HWND(), 0, 0, rect.Width, rect.Height, SET_WINDOW_POS_FLAGS.SWP_NOACTIVATE | SET_WINDOW_POS_FLAGS.SWP_NOZORDER | SET_WINDOW_POS_FLAGS.SWP_SHOWWINDOW); } }
public partial class DesktopWindow { private static readonly unsafe HINSTANCE g_hInstance = new((void*)Process.GetCurrentProcess().Handle);
private const string s_windowClassName = "WinUIDesktopWin32WindowClass";
private const string s_defaultWindowTitle = "WinUI Desktop";
private static unsafe WNDCLASSEXW RegisterDesktopWindowClass(WNDPROC lpfnWndProc) { if (!PInvoke.GetClassInfoEx(new DefaultSafeHandle(g_hInstance), s_windowClassName, out WNDCLASSEXW wndClassEx)) { wndClassEx.cbSize = (uint)Marshal.SizeOf(wndClassEx); wndClassEx.style = WNDCLASS_STYLES.CS_HREDRAW | WNDCLASS_STYLES.CS_VREDRAW; wndClassEx.cbClsExtra = 0; wndClassEx.cbWndExtra = 0; wndClassEx.hCursor = PInvoke.LoadCursor(new HINSTANCE(), PInvoke.IDC_ARROW); wndClassEx.hbrBackground = (HBRUSH)((nint)SYS_COLOR_INDEX.COLOR_WINDOW + 1); wndClassEx.hInstance = g_hInstance;
fixed (char* lps_windowClassName = s_windowClassName) { wndClassEx.lpszClassName = lps_windowClassName; }
wndClassEx.lpfnWndProc = lpfnWndProc; _ = PInvoke.RegisterClassEx(wndClassEx);
return wndClassEx; } return default; }
private static unsafe HWND CreateDesktopWindow() => PInvoke.CreateWindowEx( 0, s_windowClassName, s_defaultWindowTitle, WINDOW_STYLE.WS_OVERLAPPEDWINDOW | WINDOW_STYLE.WS_VISIBLE, int.MinValue, (int)SHOW_WINDOW_CMD.SW_HIDE, int.MinValue, int.MinValue, new HWND(), null, new DefaultSafeHandle(g_hInstance), null);
private partial class DefaultSafeHandle(nint invalidHandleValue, bool ownsHandle) : SafeHandle(invalidHandleValue, ownsHandle) { public DefaultSafeHandle(nint handle) : this(handle, true) => SetHandle(handle);
public override bool IsInvalid => handle != nint.Zero;
protected override bool ReleaseHandle() => true; } }
public partial class DesktopWindow { public static Task<DesktopWindow> CreateAsync(Action<DesktopWindowXamlSource> launched) { TaskCompletionSource<DesktopWindow> taskCompletionSource = new();
new Thread(() => { try { DesktopWindowXamlSource source; using (HookWindowingModel hook = new()) { source = new DesktopWindowXamlSource(); }
DesktopWindow window = new() { WindowXamlSource = source };
launched(source); taskCompletionSource.SetResult(window);
MSG msg = new(); while (msg.message != PInvoke.WM_QUIT) { if (PInvoke.PeekMessage(out msg, new HWND(), 0, 0, PEEK_MESSAGE_REMOVE_TYPE.PM_REMOVE)) { _ = PInvoke.DispatchMessage(msg); } } } catch (Exception e) { taskCompletionSource.SetException(e); } }) { Name = nameof(DesktopWindowXamlSource) }.Start();
return taskCompletionSource.Task; } }
namespace Windows.Win32.System.WinRT.Xaml { [GeneratedComInterface] [Guid("3CBCF1BF-2F76-4E9C-96AB-E84B37972554")] internal partial interface IDesktopWindowXamlSourceNative { [PreserveSig] [return: MarshalAs(UnmanagedType.Error)] int AttachToWindow(nint parentWnd);
[PreserveSig] [return: MarshalAs(UnmanagedType.Error)] int get_WindowHandle(out nint hWnd); }
file static class Extensions { public static HWND WindowHandle(this IDesktopWindowXamlSourceNative source) { _ = source.get_WindowHandle(out nint hWnd); return new HWND(hWnd); } } }
|