Skip to content

Instantly share code, notes, and snippets.

@SolindekDev
Created December 17, 2021 14:23
Show Gist options
  • Save SolindekDev/78ee7bcf800dae7d18704158b39d62ea to your computer and use it in GitHub Desktop.
Save SolindekDev/78ee7bcf800dae7d18704158b39d62ea to your computer and use it in GitHub Desktop.
WinAPI example window
#include <windows.h>
#define TITLE L"Example Window Title"
#define WIDTH 800
#define HEIGHT 600
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, int nCmdShow) {
MSG msg;
HWND hwnd;
WNDCLASSW wc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.lpszClassName = L"Window";
wc.hInstance = hInstance;
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
wc.lpszMenuName = NULL;
wc.lpfnWndProc = WndProc;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
RegisterClassW(&wc);
CreateWindowW(wc.lpszClassName, TITLE,
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
100, 100, WIDTH, HEIGHT, 0, 0, hInstance, 0);
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg,
WPARAM wParam, LPARAM lParam) {
switch(msg) {
case WM_CREATE:
CenterWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProcW(hwnd, msg, wParam, lParam);
}
void CenterWindow(HWND hwnd) {
RECT rc = {0};
GetWindowRect(hwnd, &rc);
int win_w = rc.right - rc.left;
int win_h = rc.bottom - rc.top;
int screen_w = GetSystemMetrics(SM_CXSCREEN);
int screen_h = GetSystemMetrics(SM_CYSCREEN);
SetWindowPos(hwnd, HWND_TOP, (screen_w - win_w)/2,
(screen_h - win_h)/2, 0, 0, SWP_NOSIZE);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment