win32/open_window Windows

Code

main.odin ¶
64 linesSource

1package open_win32_window
2
3import win "core:sys/windows"
4import fmt "core:fmt"
5import os "core:os"
6
7main :: proc() {
8	// This isn't exactly equivalent to getting the hInstance argument passed to wWinMain in C,
9	// but it's good enough for all intents and purposes.
10	instance := win.HINSTANCE(win.GetModuleHandleW(nil))
11	assert(instance != nil, "Failed to fetch current instance")
12	// The 'hPrevInstance' parameter is always nil in modern Windows applications.
13
14	// Equivalent code to getting the lpCmdLine argument passed to wWinMain in C
15	lpCmdLine := win.GetCommandLineW()
16	fmt.printfln("Command line used to start this application was: %v", lpCmdLine)
17
18	// Equivalent code to getting the nCmdShow argument passed to wWinMain in C
19	startup_info : win.STARTUPINFOW
20	win.GetStartupInfoW(&startup_info)
21	nCmdShow := (startup_info.dwFlags & win.STARTF_USESHOWWINDOW) != 0 ? cast(win.c_int)startup_info.wShowWindow : win.SW_SHOWDEFAULT
22
23	CLASS_NAME :: "Windows Window"
24
25	cls := win.WNDCLASSW {
26		lpfnWndProc = win_proc,
27		lpszClassName = CLASS_NAME,
28		hInstance = instance,
29		hCursor = win.LoadCursorA(nil, win.IDC_ARROW),
30		hbrBackground = cast(win.HBRUSH)cast(uintptr)(win.COLOR_WINDOW + 1),
31	}
32
33	class := win.RegisterClassW(&cls)
34	assert(class != 0, "Class creation failed")
35
36	hwnd := win.CreateWindowW(CLASS_NAME,
37		win.L("Windows Window"),
38		win.WS_OVERLAPPEDWINDOW,
39		win.CW_USEDEFAULT, 0, 1280, 720,
40		nil, nil, instance, nil)
41
42	assert(hwnd != nil, "Window creation Failed")
43
44	win.ShowWindow(hwnd, nCmdShow)
45	win.UpdateWindow(hwnd)
46
47	msg: win.MSG
48
49	for	win.GetMessageW(&msg, nil, 0, 0) > 0 {
50		win.TranslateMessage(&msg)
51		win.DispatchMessageW(&msg)
52	}
53
54	os.exit(cast(int)msg.wParam)
55}
56
57win_proc :: proc "stdcall" (hwnd: win.HWND, msg: win.UINT, wparam: win.WPARAM, lparam: win.LPARAM) -> win.LRESULT {
58	switch(msg) {
59	case win.WM_DESTROY:
60		win.PostQuitMessage(0)
61	}
62
63	return win.DefWindowProcW(hwnd, msg, wparam, lparam)
64}

Declarations Used 34