Sign in to follow this  
KingSatchmo

Why are pens and brushes not working?

Recommended Posts

KingSatchmo    122
Hiya, I am trying to learn pens and brushes in windows in C++ and they don't seem to be working... Here is my code:
Quote:
void GamePaint(HDC hDC) { HPEN hBluePen = CreatePen(PS_SOLID, 1, RGB (0,0,255)); HPEN hPen = SelectObject (hDC, hBluePen); Rectangle (hDC, 16, 36, 72, 70); SelectObject (hDC, hPen); DeleteObject (hDC, hBlue Pen); }
and Dev-Cpp returns these errors:
Quote:
C:\Dev-Cpp\New Folder\Practice.cpp In function `void GamePaint(HDC__*)': 53 C:\Dev-Cpp\New Folder\Practice.cpp invalid conversion from `void*' to `HPEN__*' 56 C:\Dev-Cpp\New Folder\Practice.cpp `hBlue' undeclared (first use this function) (Each undeclared identifier is reported only once for each function it appears in.) C:\Dev-Cpp\Makefile.win [Build Error] ["New Folder/Practice.o"] Error 1
I am not really sure what those mean. Any help?

Share this post


Link to post
Share on other sites
SiCrane    11839
You usually need to use explicit casts to get handle types to assign properly with GDI functions in C++ (though you can try using windowsx.h instead of windows.h, which is supposed to help with that somewhat). The second errors is because you split hBluePen into hBlue Pen in the DeleteObject() call.

Share this post


Link to post
Share on other sites
KingSatchmo    122
Quote:
Original post by SiCrane
You usually need to use explicit casts to get handle types to assign properly with GDI functions in C++


I understand what you are saying but I don't know how to fix that.

Share this post


Link to post
Share on other sites
SiCrane    11839
Basically you need to put casts in your code.

HPEN hPen = static_cast<HPEN>(SelectObject (hDC, hBluePen));

(Admittedly C style casts are more common in GDI code.)

Share this post


Link to post
Share on other sites
Anon Mike    1098
Forget about casting, include windowsx.h right after windows.h, and change your code to:


void GamePaint(HDC hDC)
{
HPEN hBluePen = CreatePen(PS_SOLID, 1, RGB (0,0,255));

HPEN hPen = SelectPen (hDC, hBluePen);
Rectangle (hDC, 16, 36, 72, 70);
SelectPen (hDC, hPen);
DeletePen (hBluePen);
}



(untested, I don't have access to a compiler at the moment, but you should get the idea)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this