Jump to content
  • Advertisement
Sign in to follow this  
Fixxer

Problem with drawing lines: Win GDI

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I get this error from the code below, copied directly from my book. ------ Build started: Project: Example 2, Configuration: Debug Win32 ------ Compiling... main.cpp .\main.cpp(175) : error C2440: 'initializing' : cannot convert from 'HGDIOBJ' to 'HPEN' Conversion from 'void*' to pointer to non-'void' requires an explicit cast Build log was saved at "file://c:\Documents and Settings\Owner\Desktop\Programming\Windows Game Programming Book\Chapter 4\Example 2\Example 2\Debug\BuildLog.htm" Example 2 - 1 error(s), 0 warning(s) ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
void GameMain(HDC hdc, HWND hwnd)
{
	if(KEYDOWN(VK_ESCAPE))
		SendMessage(hwnd,WM_CLOSE,0,0);
	
	// create random pen
	HPEN rand_pen = CreatePen(PS_SOLID,1,RGB(rand()%255, rand()%255, rand()%255));

	// select the pen into context
	HPEN old_pen = SelectObject(hdc,rand_pen);  // LINE 175 *********

	// draw the line
	MoveToEx(hdc,rand()%400,rand()%400,NULL);
	LineTo(hdc,rand()%400,rand()%400);

	// restore old pen
	SelectObject(hdc,old_pen);

	// delete pen
	DeleteObject(rand_pen);
}


Share this post


Link to post
Share on other sites
Advertisement
SelectObject returns an HGDIOBJ, not an HPEN. You need to cast it. Change the line to:
HPEN old_pen = (HPEN)SelectObject(hdc,rand_pen);

EDIT: A little offtopic, but you should avoid creating and destroying objects every frame like that. You should create it at startup and delete it at shutdown.

EDIT 2: Also, those colour values for the pen should ideally be rand()%256, otherwise you'll never get a completely white (255, 255, 255) pen.

Share this post


Link to post
Share on other sites
when you get these types of errors:
.\main.cpp(175) : error C2440: 'initializing' : cannot convert from 'HGDIOBJ' to 'HPEN'
Conversion from 'void*' to pointer to non-'void' requires an explicit cast

you need to do a cast, such as the one below.
HPEN old_pen = (HPEN)SelectObject(hdc,rand_pen);

edit: i've been beaten... arggggggh

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!