• ### What is your GameDev Story?

Public Group

#### Archived

This topic is now archived and is closed to further replies.

# Newbie::help -> Trying to make a hello world GDI/CPen demo

This topic is 5823 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi. I''m coming over to C++ from the land of C and QBasic. Actually, Windows programming itself is so forign to me. It seems like its alot harder to learn than it should be. I have some Win32API referance (PDF docs) and the MSDN Library that I have been trying to learn from but I am having alot of trouble. Take the following function for example.
void draw_stuff(void)
{
HPEN hpen;
RECT rect;
CPen myPen;

/*
**  Make a pen and Prompt if it dosent work
*/
myPen.CreatePen(PS_SOLID, 0, RGB(0,255,0));
if (!hpen)
{
if (MessageBox(hWnd, "CreatePen() failed. Would you like to continue?",
"ERROR", MB_YESNO | MB_ICONQUESTION | MB_DEFBUTTON2) == IDNO)
{
PostQuitMessage(0);
}
};
hpen= (HPEN) myPen;  // cast CPEN to a HPEN

//  Delete the pen
DeleteObject(hpen);
}

c:\program files\microsoft visual studio\myprojects\ass_roids\starfield.cpp(177) : error C2065: ''CPen'' : undeclared identifier
c:\program files\microsoft visual studio\myprojects\ass_roids\starfield.cpp(177) : error C2146: syntax error : missing '';'' before identifier ''myPen''
c:\program files\microsoft visual studio\myprojects\ass_roids\starfield.cpp(177) : error C2065: ''myPen'' : undeclared identifier
c:\program files\microsoft visual studio\myprojects\ass_roids\starfield.cpp(183) : error C2228: left of ''.CreatePen'' must have class/struct/union type
Error executing cl.exe.
Creating browse info file...

ass_roids.exe - 4 error(s), 0 warning(s)

FIrst of all, CPen is a object and hpen is a structure. I have gotten this much. I dont understand why, but I am guessing you have to cast these back and forth to select them into DC''s and stuff. Anyways, im really confused. Can somebody halp me with some info or a link to a good tutorial or somthing? PS. I have managed to get some dots on the screen VIA SetPixel(). I even made a line drawing routine with it. I know that there are many primitives built into the GDI and even BitBlt which I am desperatly trying to wade to. - Twisted Matrix

##### Share on other sites
Most win32 classes (CPen, CWnd etc.) are basically wrappers around windows structures (HPEN and HWND etc.) For the most part you''ll probably want to stick to using the CPen etc. classes, and only touch the underlying structs if absolutly nessisary. Also, theres often overloaded operators that allow (for example) a CPen to be used in functions that expect a HPEN.

Your code has a whole heap of problems - for a start just use CPen and let it manage the HPEN stuff. When you call myPen.create you tell it to create its own HPEN, it won''t affect your hpen variable.

##### Share on other sites
Generally, casting of that sort is considered bad practice. If you think you need to do it to get some windows code to work, you need to read the docs again.

To be honest, the code you posted just doesn't make any sense.

EDIT: Apparently, you just didn't include whatever .h file you need that contains the CPen class. That's what's causing your compile time errors. But if you actually get it to compile and run, that code will not work because it's full of runtime errors.

1) The if(!hpen) clause doesn't make sense because hpen will never be initialized; you aren't doing anything to initialize it.

2) Don't cast CPen to HPEN

3) Even if you were supposed to use an HPEN for this example, why do you delete it before you even use it?

[edited by - micepick on February 8, 2003 11:50:20 AM]

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 13
• 9
• 9
• 15
• 14
• ### Forum Statistics

• Total Topics
634070
• Total Posts
3015335
×