Jump to content
  • Advertisement

Archived

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

Fredric

Drawing a line using the GDI

This topic is 6928 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''m having a really crap time compiling and building the following chunk from my WinProc: case WM_PAINT: hdc = GetDC(hwnd); HPEN green_pen = CreatePen(PS_SOLID, 1, RGB(0, 255, 0)); HPEN main_pen = SelectObject(hdc, green_pen); // draw a line MoveToEx(hdc, 10, 10, NULL); LineTo(hdc, 50, 80); SelectObject(hdc, main_pen); DeleteObject(green_pen); ReleaseDC(hwnd, hdc); break; could anyone tell me what''s wrong with it? It''s straight out of a book, so I''m also having a hard time understanding why the code was used the way it is. It''s all fine up to the point where: HPEN main_pen = SelectObject(hdc, green_pen). That means that the main_pen is basically holding the green pen. Hehe, I can''t explain it well, but I know what it means. The line is drawn, so does that mean when I put HPEN main_pen = SelectObject(hdc, green_pen), it makes the object that the GDI is working with is the green_pen? Alright, and the next confusion I have is why in the world this would be put: SelectObject(hdc, main_pen); What''s the point?! The line has been drawn, and main_pen didn''t really do ANYTHING for the entire procedure. This is totally confusing me! GO LEAFS GO!

Share this post


Link to post
Share on other sites
Advertisement
Hi!

SelectObject(hdc, green_pen) selects the green_pen into the dc and returns the previous pen. Then the line is drawn using the green_pen. The call to SelectObject(hdc, main_pen) at the end restores the old pen. Restoring the old pen isn''t always necessary, but it is good custom to do so.

*Sparkle*

Share this post


Link to post
Share on other sites
This code draws a line without errors I think:

HPEN hPen = CreatePen(nPenStyle,nWidth,crColor);
SelectObject(hdc,hPen);
MoveToEx(hdc,x,y,NULL);
LineTo(hdc,xcoord,ycoord);
DeleteObject(hPen);


Visit our homepage: www.rarebyte.de.st

GA

Share this post


Link to post
Share on other sites
By the way, if it won''t compile, try some type casting:

HPEN main_pen = (HPEN)SelectObject(hdc, green_pen);

*Sparkle*

Share this post


Link to post
Share on other sites
Generally, many programmers don''t use the Win32 GDI because it is slow, however...

This code is changing the PEN to green_pen, then after it
draw''s its line it changes the PEN back to its original state (main_pen). And then destroys the green_pen.

Here is the solution:

// WndProc
{
HPEN green_pen;
HPEN main_pen;
switch....
case WM_PAINT:
hdc = GetDC(hwnd);
green_pen = CreatePen(PS_SOLID, 1, RGB(0, 255, 0));
main_pen = (HPEN)SelectObject(hdc, green_pen);

// draw a line
MoveToEx(hdc, 10, 10, NULL);
LineTo(hdc, 50, 80);
SelectObject(hdc, main_pen);
DeleteObject(green_pen);

ReleaseDC(hwnd, hdc);
break;
...
}

The causes of you problems are this:

1) You are declaring a variable in a switch-case statement, which means it sometimes is not allocated (depending on whether the statement is true).

1 Solution) Move the WM_PAINT: to a seperate function OR declare the variables before the switch-case statements (at the start of the function).

2) SelectObject returns a void *, which has to be type casted to a HPEN. hence the (HPEN) after the equal sign.

Other than that the code compiles perfectly and runs on my test app.

If that doesn''t work reply, and tell me the error messages and I will see if I can help you out.

Share this post


Link to post
Share on other sites
Well I guess some other people beat me to it, however the scope of your variables no one mentioned.... So, not all a loss.

Have Fun with your program/game!

Share this post


Link to post
Share on other sites
What caught my eye is that since your in the WM_PAINT message, where is says GetDC() at the begining, i would change it to BeginPaint. Just a though, i dont know if there''ll be a difference. Dont forget to do EndPaint at the end, hehe . I just always saw it that way in WM_PAINT.

Share this post


Link to post
Share on other sites
heh, I know that the GDI is too slow to use for any game, but since it's being taught in the book, I assume that I should learn it before I go into the chapters that teach Dx. Otherwise, I wouldn't be spending my time Win32 programming at all! Thanks all of you, it worked great!
But on the other hand... funny how that wasn't shown in the book! Bastard...

GO LEAFS GO!

Edited by - Fredric on 4/29/00 11:06:24 AM

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!