• 10
• 9
• 13
• 10
• 18

winApi paint

This topic is 4560 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I have just begun learning the WinApi and I have made a small program where I have an edit box that I type something into and then press a button and I display the text I typed in on the screen. I use this to paint it to the screen:

hdc = BeginPaint( hWnd, &ps );

SetTextColor(hdc,0x00197422);//0x00bbggrr
TextOut(hdc, 190,100, testStr, 4);

EndPaint(hWnd, &ps);


If I try to type something else in and push the button nothing happens it only works that first time. Same thing if I already have some text painted to the screen. I know this is probably a simple thing but I can't figure out what I need to do.

Share on other sites
You have to call InvalidateRect() before BeginPaint() because it excludes regions that need not to be updated.

Actually, when you know you need to paint to a window, you should use GetDC() and paint using that HDC (don't forget to ReleaseDC() it after it's needed no more).

BeginPaint() should only be used when a WM_PAINT message is sent to the window.

Read the remarks of the BeginPaint() function at MSDN for more info.

Share on other sites
The whole BeginPaint...EndPaint block should be in a WM_PAINT handler, not elsewhere. To make sure the WM_PAINT handler is called, use
RedrawWindow (hWnd, NULL, NULL, RDW_ERASE | RDW_INVALIDATE);

Edit: kamikaze beat me to it...

Share on other sites
Kippesoep your thing worked. I put the begin paint and all that in WM_PAINT. I am still a little confused about the invalidate rect stuff. I don't have that and it works with what Kippesoep said. I put RedrawWindow (hWnd, NULL, NULL, RDW_ERASE | RDW_INVALIDATE); int the code that runs when the press the button. Should that be where I should put it?

Share on other sites
The call is in the right place. You don't need the InvalidateRect call because RedrawWindow is doing that for you.

Share on other sites
As a minor aside you can use the RGB macro to make things a little more readable instead of hardcoding mysterious hex numbers that need to be explained. e.g.

RGB(34, 116, 25)

0x00197422 //0x00bbggrr

Share on other sites
So am I going to have to write redraw window after everything that is supposed to change the screen? And thanks I will changet hat anon mike.

Share on other sites
If you use BeginPaint(), it will exclude areas of the window that need not to be updated, therefore, if you're trying to paint to the HDC it provides and the window's rect has been validated before (using EndPaint() or ValidateRect()) it won't paint anything at all.
However you can get a HDC that won't exclude any area of the window using GetHDC().

So, if you really need to repaint the window you can do either this:
InvalidateRect( hWnd, NULL );BeginPaint(...);... // paint your stuff hereEndPaint(...);

or this:
HDC hWndDC = GetDC( hWnd );... // paint your stuff hereReleaseDC( hWndDC );

I recommend using the InvalidateRect method though.