Drawtext function

Hi,I have one doubt about drawtext function.Drawtext function is using for printing some text on the screen of specified rectangle,i think.my doubt is, can i use the same function for clearing that rectangle,ie,First time calling drawtext will draw the text on the screen,second time call drawtext will clear that screen(specified RECT).am i correct??
Technically that's possible... but it's a bad idea.

Conceptually you seem to be confused about something. Screen drawing is done in the WM_PAINT handler. WM_PAINT occurs after the background is erased (unless you specifically say that you do not want the background erased). So by the time you are drawing, the screen (and anything on it) is already cleared. So there's nothing for you to erase.

To "hide" text... you simply do not draw it, then force the window to refresh... causing another WM_PAINT to occur.

Here's a basic example (note: untested and functions are from memory so I might have some slight errors):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
bool drawTextFlag = false;  // <- some kind of flag to indicate whether or not you want text visible

//...

LRESULT CALLBACK WndProc(HWND wnd, UINT msg, WPARAM w, LPARAM l)
{
    switch(msg)
    {
    case WM_PAINT:
    {
        // when drawing...
        PAINTSTRUCT ps;
        HDC dc = BeginPaint( wnd, &ps );

        // do your drawing to 'dc' here
        if( drawTextFlag )
            DrawText( ... );  // <- draw whatever text you want,
                  // ONLY IF the drawTextFlag variable is true


        // when done drawing...
        EndPaint( wnd, &ps );
    }break;


    case WM_KEYDOWN:
        // Press the "Space" bar to make the text visible
        // Press the "Escape" key to hide it again
        switch(w)
        {
        case VK_SPACE:
            drawTextFlag = true; // <- make the text visible
            InvalidateRect( wnd, NULL, true );  // <- force the screen to redraw
            break;

        case VK_ESCAPE:
            drawTextFlag = false; // <- hide the text
            InvalidateRect( wnd, NULL, true );  // <- force the screen to redraw
            break;

//... 
can i use the same function for clearing that rectangle, ...

Out of interest, do you have a particular use in mind for this trick?

Andy
Thank you for yor information.In your sample code you are calling a function InvalidateRect() will refresh the screen.After that, for going control again to the WM_PAINT handler UpdateWindow()function needed or not?
Last edited on
UpdateWindow()function needed or not?

InvalidateRect() should generally be used rather than UpdateWindow()

You should only use UpdateWindow() when you need your window to be repainted immediately. It causes a WM_PAINT message to be sent directly to your app, rather than though the message queue.

InvalidateRect() marks a region as invalid so it will included when the next WM_PAINT is sent. This allows the system to repaint the whole screen more efficiently by batching the repaint calls. One effect of this batching process is that multiple calls to InvalidateRect() can lead to just a single WM_PAINT message.

The one place it is standard practice to call UpdateWindow() is immediately after showing the main window in WinMain.

Andy
Last edited on
Topic archived. No new replies allowed.