Repaint Windows Example

#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
const wchar_t CLASS_NAME[] = L"WindowClass";////in modern C and C++ declare string literals as const

WNDCLASSEXW wc = {0};
MSG msg;

//Registering the Window Class
wc.cbSize = sizeof(WNDCLASSEXW);
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);
RegisterClassExW(&wc);

//Creating the Window
CreateWindowExW(WS_EX_CLIENTEDGE, CLASS_NAME, L"Repaint Demo", WS_VISIBLE | WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 340, 220, NULL, NULL, hInstance, NULL);

//The Message Loop
while (GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return static_cast<int>(msg.wParam);
}

int paintCount = 0;

//WndProc procedure. Application acts on messages

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{

switch (msg)
{
case WM_SIZE:
InvalidateRect(hwnd, NULL, TRUE); // force repaint
return 0;

case WM_PAINT:
{
paintCount++;

PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);


// display repaint count
wchar_t buffer[64];
wsprintfW(buffer, L"WM_PAINT count: %d", paintCount);

TextOutW(hdc, 20, 20, buffer, lstrlenW(buffer));

EndPaint(hwnd, &ps);
return 0;
}

case WM_DESTROY:
PostQuitMessage(0);
return 0;
}

return DefWindowProcW(hwnd, msg, wParam, lParam);
}