Question

So I'm creating a Windows application with WinAPI, and I'm using the TextOut() function while handling WM_PAINT in order to write text on the window.

case WM_PAINT:
{
     PAINTSTRUCT ps;
     HDC hdc;
     hdc = BeginPaint(hwnd,&ps);
     SelectObject(hdc,hfDefault);
     TextOut(hdc,10,70,"Points: 0",9);
     TextOut(hdc,10,85,"Level: 0",8);
     // ...
     EndPaint(hwnd,&ps);
}
break;

How can I change the text printed by TextOut() after the function is called as well as the last parameter that determines the length of the printed text?

I have Googled about this, but for my surprise I could not find anything related. Everything asked about TextOut() was about the text font.

Was it helpful?

Solution

Maybe something like this....

// I'll assume hwnd is global
void OnSomeActionToRefreshValues()
{
    HDC hdc = ::GetDc(hwnd);
    DrawValues(hdc, 88, 99);
    ReleaseDC(hdc);
}

void DrawValues(HDC hdc, int points, int level)
{
    // Might need a rectangle here to overwrite old text
    SelectObject(hdc, hfDefault);    // I assume hfDefault is global
    TCHAR text[256];
    swprintf_s(text, 256, L"Points: %d", points);
    TextOut(hdc, 10, 70, text, wcslen(text));
    swprintf_s(text, 256, L"Level: %d", level);
    TextOut(hdc, 10, 85, text, wcslen(text));
}

And in you win proc:

case WM_PAINT:
    PAINTSTRUCT ps;
    HDC hdc;
    hdc = BeginPaint(hwnd,&ps);
    DrawValues(hdc, 88, 99);
    EndPaint(hwnd,&ps);
    break;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top