Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GDI problem: CreateSolidBrush

Status
Not open for further replies.

sandup

Technical User
Jun 7, 2003
24
RO
Hi,

I'm writing a program that updates the client area (fills it with a new color) at each WM_MOUSEMOVE.

I use CreateSolidBrush, fill the area, then duly DeleteObject the brush, but still it doesn't work - after a few hundred mousemoves the brush turns white and stays white -- my guess GDI runs out of handles of something.

What's the problem? I delete the brush immediately after creating it.
 
do you want to write some actual code here? that may help

i'm not an expert on these things, but have you checked to make sure you are releasing the DC at the correct time?

good luck, this stuff ain't easy...

-------------------------------------------------
“Talent is cheap, dedication is expensive. It will cost you your life.”
 
Do you also reselect the previous brush into the device context, before you delete the solid brush you created?

Greetings,
Rick
 
Since you are in the mouse move handler, you needed to created a device context. You must also release the device context object with a call to ReleaseDC.

If you fail to do this you will run out of GDI resources and your program will fail.
 
Here is the code. After about 900 mousemoves the rectangle goes white and stays white.

case WM_MOUSEMOVE:
xMouse = LOWORD(lParam);
yMouse = HIWORD(lParam);
InvalidateRect(hwnd,NULL,FALSE);
return 0;

case WM_PAINT :
hdc = BeginPaint (hwnd, &ps) ;

SelectObject(hdc, CreateSolidBrush (CalculateColor(xMouse, yMouse)));
GetClientRect (hwnd, &rect) ;
Rectangle (hdc, 0, 0, rect.right, rect.bottom); //fill client area

TextOut (hdc, 10, 10, szBuffer, wsprintf(szBuffer, "Mousemoves=%d", ++moves));

EndPaint (hwnd, &ps) ;

DeleteObject (SelectObject(hdc, GetStockObject(WHITE_BRUSH)));
return 0 ;

I now realized my mistake was deleting the object *after* EndPaint. Technically this isn't wrong since Petzold says GDI objects are independent of DC's and can be created/destroyed any time. The moral is, destroy your GDI objects *before* closing the DC.

If I move DeleteObject in front of EndPaint, it works fine forever.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top