Archived

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

Megatron

Drawing with Lines ... strange error.

Recommended Posts

I''m not sure why this is giving me problems (obviously). Here is the code, and here is the error. CODE: ... ... hdc = GetDC(hwnd); // create a random colored pen HPEN hpen = CreatePen(PS_SOLID,1,RGB(255,0,0)); // select the pen into context LINE IN QUESTION (208)->HPEN old_pen = SelectObject(hdc, hpen); // move to a random postion MoveToEx(hdc, 0,0, NULL); // draw a line LineTo(hdc,90,90); // now delete the pen SelectObject(hdc,old_pen); DeleteObject(hpen); // release the device context ReleaseDC(hwnd,hdc); ERROR: E:\VisualC++\MyProjects\Good\Good2.cpp(208) : error C2440: ''initializing'' : cannot convert from ''void *'' to ''struct HPEN__ *'' Conversion from ''void*'' to pointer to non-''void'' requires an explicit cast

Share this post


Link to post
Share on other sites
SelectObject() returns a (void *). If you have defined STRICT, or you are using MFC, then you cannot freely convert between (void *), HPEN, HBRUSH, etc - they''re all different types. You need to explicitly cast, so replace that line with:

  
HPEN old_pen = (HPEN)SelectObject( hdc, hpen );


and it should work!


War Worlds - A 3D Real-Time Strategy game in development.

Share this post


Link to post
Share on other sites
Adding to Dean Harding''s answer...

If you are using MSVC++ 6.0, instead of an earlier version, Microsoft added stricter type checking in version 6.0, so you have to do a typecast when converting from void *''s to types like HPEN and HBRUSH.

Add the (HPEN) in front like Dean Harding said and it should work fine.

-Hyren




"Back to the code mines... ka-chink... ka-chink..."
vidgamez.iwarp.com

Share this post


Link to post
Share on other sites
Thanks guys...it works great. FYI this is going to really put me on guard when reading "Tricks of the Windows Game Programming Gurus."

Share this post


Link to post
Share on other sites