In order to update the displayed text in a window when handling the WM_PAINT
message, you will need to have some source for the text string to be displayed.
Since your original post is somewhat old, the Windows API has changed with new versions of Windows, with the current version of Windows 10 and Windows 11 already in beta.
Windows since Windows XP is 16 bit UNICODE for the WinAPI so people mostly use wchar_t
text characters. This requires that text character string constants need the L
modifier as in L"wchar_t text"
.
Using Visual Studio 2019, I put together a simple example that runs with Windows 10. This is a simple Windows WinAPI desktop GUI application. I started with a new project in Visual Studio and had the IDE generate the skeleton for a Windows Desktop GUI application with the wWinMain()
, MyRegisterClass()
, InitInstance()
, and WndProc()
.
Then I modified that generated source to do the following:
- present in the main window four buttons to allow data changes
- display two text strings which are updated with counts for button clicks
I elected to use the default font so did not do anything to modify the font used to display the text. If you need to modify the font, you will need to add the code to create the font you want, select the new font into the HDC
for drawing the text, then use TextOut()
to draw the text with the new font. After using the font you would need to swap it back out and then delete it.

The first step was to create a data area for managing the buttons and button clicks. I chose to create the buttons in the InitInstance()
.
static struct {
const wchar_t* txt; // pointer to text to display on button face
int iCount; // count of number of times button clicked
HWND hwnd; // button window handle which identifies the button
} myButtons[] = {
{L"Points up", 0, 0},
{L"Points dwn", 0, 0},
{L"Level up", 0, 0},
{L"Level dwn", 0, 0}
};
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // Store instance handle in our global variable
HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
if (!hWnd)
{
return FALSE;
}
// create the displayed window along with the buttons.
// the buttons are in a single row at the top of the window.
POINT myPoint = { 10, 10 }; // x, y
for (auto &a : myButtons) {
a.hwnd = CreateWindow(
L"BUTTON", // Predefined class; Unicode assumed
a.txt, // Button text
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, // Styles
myPoint.x, // x position
myPoint.y, // y position
100, // Button width
50, // Button height
hWnd, // Parent window
NULL, // No menu.
(HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE),
NULL); // Pointer not needed.
myPoint.x += 100 + 20; // button width plus a separation distance
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
The source code for updating the displayed window follows. We have two functions, a button click handler to determine which button was clicked and the WndProc()
with the WM_PAINT
message handler which modifies the displayed window.
// process a button click event and return an indication
// whether the button handle matches one we are managing (1)
// or not managing (0).
int buttonClick(HWND hWnd, HWND hButton)
{
// look through the list of buttons to see if the window handle
// of the button event matches one of our buttons.
for (auto &a : myButtons) {
if (a.hwnd == hButton) {
// this is one of our buttons so we increment button click count.
// then invalidate the window area and update to trigger WM_PAINT message.
a.iCount++;
InvalidateRect(hWnd, NULL, TRUE);
UpdateWindow(hWnd);
return 1; // indicate we processed this event.
}
}
return 0; // indicate we did not process this event
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
int wmCode = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
// not a menu event so see if it is a button click or not.
if (wmCode == BN_CLICKED) {
// if we are managing this button then we skip
// the DefWindowProc() otherwise it is called.
if (buttonClick(hWnd, (HWND)lParam))
break;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code that uses hdc here...
// create the text strings we are going to display/update
wchar_t myText[2][64];
// following swprintf_s() works because template
// generates the proper call with the additional buffer
// size argument.
swprintf_s(myText[0], L"Points: %d", myButtons[0].iCount - myButtons[1].iCount);
swprintf_s(myText[1], L"Level: %d", myButtons[2].iCount - myButtons[3].iCount);
// get the text metrics of the font we are using to draw the text so
// that we can find out how tall the letters are and can adjust the
// distance for each line of text properly.
TEXTMETRIC myTextMetric = { 0 };
GetTextMetrics(hdc , &myTextMetric);
// we will use a POINT struct for maintaining the point at which
// the text output will start. x coordinate is horizontal position
// and y coordinate is the vertical position.
POINT myPoint = { 10, 150 }; // x, y
int myMargin = 5;
// iterate over the list of strings we are displaying and
// display each one on a separate line.
for (auto &a : myText) {
TextOut(hdc, myPoint.x, myPoint.y, a, wcslen(a));
myPoint.y += myTextMetric.tmHeight + myMargin;
}
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}