-4

I have a C++ windows API program which displays text using TextOut function

TCHAR buffer[] = _T("Hello");
        TCHAR buffer1[] = _T("How to clear this one\?");
        TextOut(hdc,200,170,buffer,_tcslen(buffer));
        TextOut(hdc, 200, 185, buffer1, _tcslen(buffer1));

My further text output has been overwritten like thisenter image description here

how to clear the previous one and add this one.

I found that doing TextOut function on the same location like this

TextOut(hdc,200,170,buffer,_tcslen(buffer));
        TextOut(hdc, 200, 170, buffer1, _tcslen(buffer1));

will replace the previous one but for some other reasons I cannot do this what is the actual way of clearing the screen. Is there anything in windows like system("cls") for console Thank you

1 Answers1

8

The question is improperly posed, as it seems that you think that the drawable surface of a window is somehow persistent - it isn't, the system is free to forget about all of its content and call your WM_PAINT handler to have it redrawn back to the state where it was. For this reason, the question feels strange: it's not that you erase some content that is already there (the window content is ephemeral), you arrange so that your paint code no longer paints what you don't want and force a repaint.

So, if you are doing your drawing in WM_PAINT like you should, you should set some kind of flag (or probably, clear the data structures that store the elements to draw) that tells your repaint code not to draw the text and do an InvalidateRect(hwnd, TRUE) to have your window background repainted and your WM_PAINT called.

My fear however is that you are not doing painting in WM_PAINT as you should, but scattered in other places using GetDC and the like (tip: in normal applications there's almost no place where you need GetDC or CreateDC); in this case, you are already doing it wrong (as you can see by minimizing and restoring your window), and you should learn how to do it properly before going on.

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
  • 1
    Related: [Difference between GetDC and BeginPaint](https://stackoverflow.com/questions/5841299/difference-between-getdc-and-beginpaint) – jrh Jun 11 '18 at 14:01