Skip to main content
GameDev.net gamedev.net
🔒 Locked

win32 api problem

Started by perrywky Oct 29, 2005 at 9:18 AM 4 replies 700+ views
Original Post
perrywky
perrywky
I've got a problem in one of my win32 application, case WM_PAINT: hdc = BeginPaint (hwnd, &ps); while (i < 20){ SelectObject (hdc, CreateSolidBrush (RGB(rand () % 256, rand () % 256, rand () % 256))); Rectangle (hdc, 3 * cxClient / 8, i * cyClient / 20, 5 * cxClient / 8, (i + 1)*cyClient / 20) ; i++; } when i resize the window,the blocks i drawed just disappeared,left only a blank in the client area,but if i don't use the loop,like SelectObject (hdc, CreateSolidBrush (RGB (rand () % 256, rand () % 256, rand () % 256))) ; Rectangle (hdc, 3 * cxClient / 8, 0 * cyClient / 20, 5 * cxClient / 8, 1 * cyClient / 20) ; SelectObject (hdc, CreateSolidBrush (RGB (rand () % 256, rand () % 256, rand () % 256))) ; Rectangle (hdc, 3 * cxClient / 8, 1 * cyClient / 20, 5 * cxClient / 8, 2 * cyClient / 20) ; SelectObject (hdc, CreateSolidBrush (RGB (rand () % 256, rand () % 256, rand () % 256))) ; Rectangle (hdc, 3 * cxClient / 8, 2 * cyClient / 20, 5 * cxClient / 8, 3 * cyClient / 20) ; draw each block manually,it shows what should be when i resize the window,i don't why,could someone kind tell me why and how i can slove this problem?
PantherBoy
PantherBoy
You sure the WM_PAINT message is called when the window is maximised? It sounds like something that should be called but you might want to check or remove it as a test
Kippesoep
Kippesoep
Wow, there's an awful lot wrong with that code. Let's see if I can sum it all up:

  • Cause of your problem: i is not initialised. Add an "i = 0;" first, or use a for loop.

  • Where's the EndPaint?

  • Continued calls to CreateSolidBrush without calling DeleteObject on the created handle -- this will exhaust the GDI pool after a whle.

  • You're not catching the result of the SelectObject and restoring it afterwards, leaving the DC in a bad state and leaking even more GDI objects



To do it right:
case WM_PAINT:   hdc = BeginPaint (hwnd, &ps);   for (i = 0; i < 20; ++i)   {      HBRUSH hbr = CreateSolidBrush (RGB(rand () % 256, rand () % 256, rand () % 256));      HGDIOBJ hold = SelectObject (hdc, hbr);      Rectangle (hdc, 3 * cxClient / 8, i * cyClient / 20, 5 * cxClient / 8, (i + 1)*cyClient / 20);      SelectObject (hdc, hold);      DeleteObject (hbr);   }   EndPaint (hwnd, &ps);   break;


Edit: added EndPaint (thanks CTar).

[Edited by - Kippesoep on October 29, 2005 10:44:13 AM]
Kippesoep
CTar
CTar
Kippesoep: In your example you are still missing EndPaint.
perrywky
perrywky
thank you very much,it's really helpful
in fact i have declared and initialised the i at the begin of the function WinProc(),but i don't know why i should initialised it in the PAINT case,
and how could i paste my code the way like yours?
Ravuya
Ravuya
Please do not double post. The original thread is here.

Thanks for the replies so far, but please take it to the other thread.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.