Skip to content

Instantly share code, notes, and snippets.

@gszauer
Created September 10, 2023 06:18
Show Gist options
  • Select an option

  • Save gszauer/8560bc64cf008ade2c80c480b6495b9a to your computer and use it in GitHub Desktop.

Select an option

Save gszauer/8560bc64cf008ade2c80c480b6495b9a to your computer and use it in GitHub Desktop.
Minimal Win32 sample
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow);
int main(int argc, char** argv) {
return WinMain(GetModuleHandle(NULL), NULL, GetCommandLineA(), SW_SHOWDEFAULT);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {
WNDCLASSA wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
wndclass.lpszMenuName = 0;
wndclass.lpszClassName = "GDI_Sample";
RegisterClassA(&wndclass);
int windowWidth = 800;
int windowHeight = 600;
RECT rClient;
SetRect(&rClient, 0, 0, windowWidth, windowHeight);
AdjustWindowRect(&rClient, WS_OVERLAPPEDWINDOW | WS_VISIBLE, FALSE);
int screenWidth = GetSystemMetrics(SM_CXSCREEN);
int screenHeight = GetSystemMetrics(SM_CYSCREEN);
windowWidth = rClient.right - rClient.left;
windowHeight = rClient.right - rClient.left;
HWND hwnd = CreateWindowA(wndclass.lpszClassName, (char*)"Window Title", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
(screenWidth / 2) - (windowWidth / 2), (screenHeight / 2) - (windowHeight / 2),
windowWidth, windowHeight, NULL, NULL, hInstance, 0);
ShowWindow(hwnd, SW_NORMAL);
UpdateWindow(hwnd);
MSG msg;
bool quit = false;
while (!quit) {
while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) {
if (msg.message == WM_QUIT) {
quit = true;
break;
}
if (msg.hwnd == hwnd) {
TranslateMessage(&msg);
DispatchMessageA(&msg);
}
}
Sleep(1);
}
return (int)msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam) {
switch (iMsg) {
case WM_CLOSE:
DestroyWindow(hwnd);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProcA(hwnd, iMsg, wParam, lParam);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment