Created
April 15, 2024 07:39
-
-
Save dgellow/e9b5eae3a41e70613aaf6ec621d1a874 to your computer and use it in GitHub Desktop.
Win32 basics in Zig. Port of https://learn.microsoft.com/en-us/windows/win32/learnwin32/your-first-windows-program
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
const w32 = @import("vendor/zigwin32/win32.zig").everything; | |
const TRUE: w32.BOOL = 1; | |
const FALSE: w32.BOOL = 0; | |
fn windowProc(handle: w32.HWND, msg: u32, wparam: w32.WPARAM, lparam: w32.LPARAM) callconv(.C) w32.LRESULT { | |
switch (msg) { | |
w32.WM_DESTROY => { | |
w32.PostQuitMessage(0); | |
return 0; | |
}, | |
w32.WM_PAINT => { | |
var paint = w32.PAINTSTRUCT{ | |
.rcPaint = w32.RECT{ | |
.left = 0, | |
.top = 0, | |
.right = 0, | |
.bottom = 0, | |
}, | |
.fErase = TRUE, | |
.hdc = null, | |
.fRestore = FALSE, | |
.fIncUpdate = FALSE, | |
.rgbReserved = .{ | |
0, | |
0, | |
0, | |
0, | |
0, | |
0, | |
0, | |
0, | |
0, | |
0, | |
0, | |
0, | |
0, | |
0, | |
0, | |
0, | |
0, | |
0, | |
0, | |
0, | |
0, | |
0, | |
0, | |
0, | |
0, | |
0, | |
0, | |
0, | |
0, | |
0, | |
0, | |
0, | |
}, | |
}; | |
const hdc = w32.BeginPaint(handle, &paint); | |
_ = w32.FillRect(hdc, &paint.rcPaint, w32.GetStockObject(w32.WHITE_BRUSH)); | |
_ = w32.EndPaint(handle, &paint); | |
return 0; | |
}, | |
else => { | |
return w32.DefWindowProcW(handle, msg, wparam, lparam); | |
}, | |
} | |
} | |
pub fn main() !u8 { | |
const h_instance = w32.GetModuleHandleW(null); | |
const class_name = w32.L("My window class"); | |
const window_class = w32.WNDCLASSW{ | |
.style = w32.WNDCLASS_STYLES{ | |
.HREDRAW = 1, | |
.VREDRAW = 1, | |
}, | |
.lpfnWndProc = windowProc, | |
.hInstance = h_instance, | |
.hCursor = null, | |
.hIcon = null, | |
.hbrBackground = null, | |
.lpszClassName = class_name, | |
.lpszMenuName = null, | |
.cbClsExtra = 0, | |
.cbWndExtra = 0, | |
}; | |
_ = w32.RegisterClassW(&window_class); | |
var style = w32.WS_OVERLAPPEDWINDOW; | |
style.VISIBLE = 1; | |
const hwnd = w32.CreateWindowExW( | |
w32.WINDOW_EX_STYLE{}, | |
class_name, | |
w32.L("Hello from zig"), | |
style, | |
w32.CW_USEDEFAULT, | |
w32.CW_USEDEFAULT, | |
640, | |
480, | |
null, | |
null, | |
h_instance, | |
null, | |
); | |
if (hwnd == null) { | |
switch (w32.GetLastError()) { | |
else => { | |
return 1; | |
}, | |
} | |
} | |
// run message loop | |
var msg = w32.MSG{ | |
.hwnd = null, | |
.lParam = 0, | |
.message = 0, | |
.pt = w32.POINT{ | |
.x = 0, | |
.y = 0, | |
}, | |
.time = 0, | |
.wParam = 0, | |
}; | |
while (w32.GetMessageW(&msg, null, 0, 0) > 0) { | |
_ = w32.TranslateMessage(&msg); | |
_ = w32.DispatchMessageW(&msg); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment