You can use Drawtext(), or TextOut(), in any moment, you just need the device context:
void PrintHello(HWND hwnd)
{
HDC dc = GetDc(hwnd);
RECT rc;
GetClientRect(hwnd, &rc);
DrawText(dc, "Hello!", -1, &rc, DT_CENTER|DT_VCENTER|DT_SINGLELINE);
ReleaseDC(hwnd, dc);
}
Then if you try to move the window, change its size, reduce to icon then revert, drag another window over it, etc. you'll see that your text is no more there.
This is because when you modify or alter in any way the window graphic area the system asks you to redraw the interested part (yes part you'll understand later) by means of the WM_PAINT message. When an application receive this message it must repaint the area.
The effective way to handle it is to create a drawing function that make all painting and call it from the WM_PAINT handler. Then force a WM_PAINT calling RedrawWindow() or UpdateWindow().
The correct way an application must handle a WM_PAINT is:
1. Call GetUpdateRect() function to check if there is any area to update. If no area is defined the function should simply return doing nothing.
2. If exist areas to redraw call BeginPaint() to obtain a PAINTSTRUCT that has again the area to paint and a device context of the window.
3. Updates the graphic area
4. Call EndPaint()
5. End of upgrade
In code:
void UpdateGraph(HWND hwnd, HDC dc)
{
RECT rc;
GetClientRect(hwnd, &rc);
DrawText(dc, "Hello!", -1, &rc, DT_CENTER|DT_VCENTER|DT_SINGLELINE);
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
switch(uMsg)
{
case WM_PAINT:
{
RECT rToPaint;
if (!GetUpdateRect(hwnd, &rToPaint, FALSE))
break; PAINTSTRUCT ps;
BeginPaint(hwnd, &ps);
UpdateGraph(hwnd, ps.hdc);
EndPaint(hwnd, &ps);
break; }
...
}
}
Why do we get a rectangle of area to print? To make graphic refresh faster, on old devices where the graphic capabilities were reduced it was very effective to limit the updating area (reducing graph actions). Nowadays when graph are managed by GPU's and no more by the CPU, this is still effective when we have complex and big graphs with high-res.
How can you manage partial drawing? Normally you create an independent bitmap and load it in a memory device context. Make all your graphic work on the latter, than when you are ready copy the memory context on the screen, using, BitBlt(), inside the WM_PAINT handler. To force posting of a
WM_PAINT message you can call
UpdateWindow() or
RedrawWindow().
In this case if an update region exist you can manage to copy only the specified part of image onto the screen device reducing update time.