Skip to content

Instantly share code, notes, and snippets.

@JNjenga
Created May 22, 2024 19:30
Show Gist options
  • Select an option

  • Save JNjenga/c07cbe99c73095434c04ae83ebc1f8c8 to your computer and use it in GitHub Desktop.

Select an option

Save JNjenga/c07cbe99c73095434c04ae83ebc1f8c8 to your computer and use it in GitHub Desktop.
Skeleton win32 app window
#ifndef UNICODE
#define UNICODE
#endif
#include <windows.h>
#pragma comment(lib, "kernel32")
#pragma comment(lib, "user32")
HWND create_window(const wchar_t* class_name, WNDPROC windproc, const wchar_t* window_name, HWND parent, HINSTANCE h_instance, int x, int y, int w, int h);
LRESULT CALLBACK windproc_callback(HWND hwnd, UINT u_msg, WPARAM w_param, LPARAM l_param);
int WINAPI wWinMain(HINSTANCE h_instance, HINSTANCE h_previnstance, PWSTR p_cmdline, int n_cmdshow)
{
const wchar_t* class_name = L"Skeleton win32 app";
const wchar_t* window_name = L"Skeleton win32 app";
HWND hwnd_parent = create_window(class_name, windproc_callback, window_name, NULL, h_instance, 0, 0, CW_USEDEFAULT, CW_USEDEFAULT);
ShowWindow(hwnd_parent, n_cmdshow);
MSG msg = {};
while(GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
HWND create_window(const wchar_t* class_name, WNDPROC windproc, const wchar_t* window_name, HWND parent, HINSTANCE h_instance, int x, int y, int w = 0, int h = 0)
{
WNDCLASS wc = {};
wc.lpfnWndProc = windproc;
wc.hInstance = h_instance;
wc.lpszClassName = class_name;
RegisterClass(&wc);
HWND hwnd = CreateWindowEx(
0,
class_name,
window_name,
WS_OVERLAPPEDWINDOW,
x ? x : CW_USEDEFAULT, y ? y : CW_USEDEFAULT,
w ? w : CW_USEDEFAULT, h ? h : CW_USEDEFAULT,
parent, // Parent
NULL,
h_instance,
NULL
);
return hwnd;
}
LRESULT CALLBACK windproc_callback(HWND hwnd, UINT u_msg, WPARAM w_param, LPARAM l_param)
{
switch(u_msg)
{
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
break;
case WM_CREATE:
{
}
break;
case WM_COMMAND:
{
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
FillRect(hdc, &ps.rcPaint, (HBRUSH)(COLOR_WINDOW + 1));
EndPaint(hwnd, &ps);
return 0;
}
break;
}
return DefWindowProc(hwnd, u_msg, w_param, l_param);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment