Why are pens and brushes not working?

Started by
3 comments, last by Anon Mike 18 years, 4 months ago
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?
Advertisement
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.
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.

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.)
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)
-Mike

This topic is closed to new replies.

Advertisement