1 /** 2 * Process-wide Win32 initialization: the module handle and the default window 3 * class registration. 4 */ 5 module deft.platform.win32.init; 6 7 version (Windows): 8 9 import core.sys.windows.windows; 10 11 import deft.platform.win32.wndproc : wndProc; 12 13 /// The name of Deft's default top-level window class (null-terminated literal). 14 enum deftWindowClassName = "DeftWindow"w; 15 16 private __gshared HINSTANCE g_hInstance; 17 private __gshared bool g_classRegistered; 18 19 /// The process module handle, fetched lazily via `GetModuleHandleW(null)`. 20 HINSTANCE hInstance() 21 { 22 if (g_hInstance is null) 23 g_hInstance = GetModuleHandleW(null); 24 return g_hInstance; 25 } 26 27 /** 28 * Register the default window class on first use. Idempotent. 29 * 30 * The class uses the master `wndProc`, redraws on resize (`CS_HREDRAW | 31 * CS_VREDRAW`), the standard arrow cursor and the system window background. 32 */ 33 void ensureWindowClass() 34 { 35 if (g_classRegistered) 36 return; 37 g_classRegistered = true; 38 39 // Default the window icon to the executable's first icon resource (id 1, the 40 // conventional application-icon id), so an app that embeds an .ico via its .rc 41 // gets it in the title bar/taskbar/Alt+Tab with no extra code. Apps can still 42 // override per-window with Window.setIcon. Null when no icon resource exists. 43 HICON appIcon = LoadIconW(hInstance(), cast(LPCWSTR) cast(void*) cast(size_t) 1); 44 45 WNDCLASSEXW wc; 46 wc.cbSize = WNDCLASSEXW.sizeof; 47 wc.style = CS_HREDRAW | CS_VREDRAW; 48 wc.lpfnWndProc = &wndProc; 49 wc.cbClsExtra = 0; 50 wc.cbWndExtra = 0; 51 wc.hInstance = hInstance(); 52 wc.hIcon = appIcon; 53 wc.hCursor = LoadCursorW(null, IDC_ARROW); 54 wc.hbrBackground = cast(HBRUSH)(COLOR_WINDOW + 1); 55 wc.lpszMenuName = null; 56 wc.lpszClassName = deftWindowClassName.ptr; 57 wc.hIconSm = appIcon; 58 59 RegisterClassExW(&wc); 60 }