Last active
October 4, 2021 17:59
-
-
Save koldev/6038760966279886b2569676428a5763 to your computer and use it in GitHub Desktop.
Create Win32 window
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
#include <windows.h> | |
LRESULT CALLBACK WndProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam) { | |
switch (Message) { | |
case WM_DESTROY: { | |
PostQuitMessage(0); | |
break; | |
} | |
default: | |
return DefWindowProc(hWnd, Message, wParam, lParam); | |
} | |
return 0; | |
} | |
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow) { | |
// Register window class | |
WNDCLASSEXW wc; | |
wc.cbSize = sizeof(WNDCLASSEXW); | |
wc.hInstance = hInstance; | |
wc.lpszClassName = L"Window Class"; | |
wc.lpfnWndProc = WndProc; | |
wc.hIcon = LoadIconW(NULL, MAKEINTRESOURCEW(32512)); | |
wc.hIconSm = LoadIconW(NULL, MAKEINTRESOURCEW(32512)); | |
wc.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(32512)); | |
wc.lpszMenuName = NULL; | |
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); | |
wc.cbClsExtra = 0; | |
wc.cbWndExtra = 0; | |
if (!RegisterClassExW(&wc)) { | |
MessageBoxW(NULL, L"Window registration failed.", L"Error", MB_ICONERROR | MB_OK); | |
return 0; | |
} | |
// Create window | |
HWND hWnd = CreateWindowExW( | |
WS_EX_OVERLAPPEDWINDOW, wc.lpszClassName, L"Window Title π ≈ 3.14", WS_OVERLAPPEDWINDOW, | |
CW_USEDEFAULT, CW_USEDEFAULT, 400, 300, | |
NULL, NULL, hInstance, NULL | |
); | |
if (hWnd == NULL) { | |
MessageBoxW(NULL, L"Window creation failed.", L"Error", MB_ICONERROR | MB_OK); | |
return 0; | |
} | |
// Show window | |
ShowWindow(hWnd, nCmdShow); | |
UpdateWindow(hWnd); | |
// Start message loop | |
MSG msg; | |
while (GetMessageW(&msg, NULL, 0, 0) > 0) { | |
TranslateMessage(&msg); | |
DispatchMessageW(&msg); | |
} | |
return msg.wParam; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
VS settings: