Skip to content

Instantly share code, notes, and snippets.

@oscarkuo
Last active April 17, 2017 19:00
Show Gist options
  • Save oscarkuo/c32aee47be4492b8d17f330af0dd7645 to your computer and use it in GitHub Desktop.
Save oscarkuo/c32aee47be4492b8d17f330af0dd7645 to your computer and use it in GitHub Desktop.
Starter code for a minimal win32 application with idle processing
#include <windows.h>
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
void RunMessageLoop(const MSG& msg);
void OnIdle(const MSG& msg);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
const wchar_t szClassName[] = L"myWindowClass";
WNDCLASSEX wc;
HWND hwnd;
MSG msg;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
wc.lpszClassName = szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
RegisterClassEx(&wc);
hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, szClassName, L"myWindowName", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 240, 120, NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
RunMessageLoop(msg);
return msg.wParam;
}
void OnIdle(const MSG& msg)
{
// Idle state processing
}
void RunMessageLoop(const MSG& msg)
{
while (WM_QUIT != msg.message)
{
while (PeekMessage(const_cast<LPMSG>(&msg), NULL, 0, 0, PM_REMOVE) > 0) //Or use an if statement
{
if (msg.message == WM_QUIT)
return;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
OnIdle(msg);
}
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
break;
case WM_CLOSE:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment