Skip to main content
gamedev.net
🔒 Locked

A problem using the SelectObject function

Started by Steve-B Aug 11, 2003 at 5:08 PM 4 replies 600+ views
Original Post
Steve-B
Steve-B
I keep getting this damn error in Visual C++ when I try to use the Select Object function. Here is the error:
error C2440: '=' : cannot convert from 'void *' to 'struct HBRUSH__ *'
        Conversion from 'void*' to pointer to non-'void' requires an explicit cast
Here is the code. The first SelectObject function is the line with the error:
   
case WM_PAINT:
			{
				PAINTSTRUCT ps;
				HBRUSH hOldBrush;

				// Start Painting

				BeginPaint(hWnd, &ps);

				// Select and use the red brush

				hOldBrush = SelectObject(ps.hdc, hRedBrush);

				// Draw a rectangle filled with the currently selected brush

				Rectangle(ps.hdc, 100, 100, 150, 150);

				// Deselect the brush

				SelectObject(ps.hdc, hOldBrush);

				// End painting

				EndPaint(hWnd, &ps);
			}
			break;
Anyone got any ideas? [edited by - Steve-B on August 11, 2003 6:10:56 PM]
CodeMunkie
CodeMunkie
Try

hOldBrush = SelectObject(ps.hdc, (HBRUSH)hRedBrush);

C++ is stricter than C and requires explicit type casts from void pointers.

[edited by - CodeMunkie on August 11, 2003 6:12:03 PM]
"When you die, if you get a choice between going to regular heaven or pie heaven, choose pie heaven. It might be a trick, but if it's not, mmmmmmm, boy."
How to Ask Questions the Smart Way.
Steve-B
Steve-B
Hi CodeMunkie. That was the first thing I tried when I got the error. Unfortunately I still get the same error.
CodeMunkie
CodeMunkie
AP is correct. You could also just cast the return type instead of changing hOldBrush''s type:

hOldBrush = (HBRUSH)SelectObject(ps.hdc, (HBRUSH)hRedBrush);
"When you die, if you get a choice between going to regular heaven or pie heaven, choose pie heaven. It might be a trick, but if it's not, mmmmmmm, boy."
How to Ask Questions the Smart Way.
Steve-B
Steve-B
Absolutely correct.

I changed the code to "HGDIOBJ hOldBrush;" and it works like a charm.

Thanks a lot!

Topic Locked

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

Sign in to reply to this topic.