repaint a window

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
case MY_BUTTON:          
                hdc = GetDC (hwnd) ;
		SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
		TextOut (hdc, 50, 130, L"1", 2);
		ReleaseDC (hwnd, hdc) ;
		ValidateRect (hwnd, &rect) ;

                break;

case WM_PAINT:

		hdc = BeginPaint (hwnd, &ps);
		SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT));
		
		TextOut (hdc, 50, 130, L"123456789", 10);
		EndPaint (hwnd, &ps);

		break;

hi,

this painted window has a text of
"123456789"

so i wanted to repaint the window with the new text
"1"


but the output is:
1 3456789


it just overlapped the first text ..how do i clear or remove the first text then repaint it again with a new text??

thanks in advance
Generally: do not draw somewhere outside WM_PAINT.

Instead store the string that you want to draw somewhere.

If you want to change that stored string you need to InvalidateRect() the current portion of the window, change the string and InvalidateRect() the new portion of the window.

InvalidateRect() can erase the background for you so no need to erase the not overwritten protion yourself. But it may cause flicker

In WM_PAINT you just need to draw the stored string.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
case MY_BUTTON:          
               
		GetClientRect(hwnd, &rect);
                InvalidateRect(hwnd, &rect, 1);

                break;

case WM_PAINT:

		hdc = BeginPaint (hwnd, &ps);
		SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT));
		
		TextOut (hdc, 120, 130, (LPCWSTR)szBUffer, 1024);
		EndPaint (hwnd, &ps);

		break;

thank you for the reply

im not really sure how to use this invalidaterect() i tried the code above, it works on repainting the winoow but i still have the ovelaping text problem
Thanks for sharing this good type of information... i need it... :)
Well, for the first attempt you may invalidate the whole client area. As long as flicker is not a problem

I don't see where you change the content of szBUffer?

Btw you need strcpy to change the content of szBUffer

This
http://msdn.microsoft.com/en-us/library/windows/desktop/dd145133%28v=vs.85%29.aspx
doesn't state that TextOut ends at zero, but likely it does

if it doesn't try TextOut (hdc, 120, 130, (LPCWSTR)szBUffer, strlen(szBUffer));
Topic archived. No new replies allowed.