Skip to content

Instantly share code, notes, and snippets.

@koldev
Last active October 4, 2021 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koldev/6038760966279886b2569676428a5763 to your computer and use it in GitHub Desktop.
Save koldev/6038760966279886b2569676428a5763 to your computer and use it in GitHub Desktop.
Create Win32 window
#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;
}
@koldev
Copy link
Author

koldev commented Oct 3, 2021

VS settings:

  • Save source file as "Unicode - Codepage 1200" if there are Unicode characters in the code.
  • Advanced > Character Set: Use Unicode Character Set
  • C/C++ > Advanced > Disable Specific Warnings: 28251
  • Linker > System > SubSystem: Windows (/SUBSYSTEM: WINDOWS)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment