Jump to content
  • Advertisement
Sign in to follow this  
Grahf750

winApi paint

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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)

instead of

0x00197422 //0x00bbggrr

Share this post


Link to post
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 this post


Link to post
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 here
EndPaint(...);

or this:

HDC hWndDC = GetDC( hWnd );
... // paint your stuff here
ReleaseDC( hWndDC );


I recommend using the InvalidateRect method though.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!