Repaint Windows Example
#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
// Global repaint counter
static int paintCount = 0;
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
const TCHAR CLASS_NAME[] = TEXT("WindowClass");
WNDCLASSEX wc;
MSG msg;
ZeroMemory(&wc, sizeof(WNDCLASSEX));
// Register the window class
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 = GetSysColorBrush(COLOR_WINDOW);
wc.lpszMenuName = NULL;
wc.lpszClassName = CLASS_NAME;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if (!RegisterClassEx(&wc))
return 0;
// Create the application window
HWND hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
CLASS_NAME,
TEXT("Repaint Demo"),
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
CW_USEDEFAULT,
CW_USEDEFAULT,
340,
220,
NULL,
NULL,
hInstance,
NULL);
if (!hwnd)
return 0;
// Message loop
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
// Window procedure
LRESULT CALLBACK WndProc(HWND hwnd,
UINT msg,
WPARAM wParam,
LPARAM lParam)
{
switch(msg)
{
case WM_SIZE:
// Force the entire client area to be repainted whenever
// the window is resized.
InvalidateRect(hwnd, NULL, TRUE);
return 0;
case WM_PAINT:
{
++paintCount;
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
TCHAR buffer[64];
wsprintf(buffer,
TEXT("WM_PAINT count: %d"),
paintCount);
TextOut(hdc,
20,
20,
buffer,
lstrlen(buffer));
EndPaint(hwnd, &ps);
return 0;
}
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}