Archived

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

Alusair

GDI Failure error from Borland C++ 5

Recommended Posts

Alusair    122
Hello. I''m somewhat new to game programming, having next to no experience with Windows programming. for the last few weeks, I''ve been reading Tricks of the Windows Game Programming Gurus and recently finished the chapters on GDI. All of these programs compile fine on my computer. However, when I tried to do some of my own work I would get an error whenever I tried to move the window or close it. When I do either I get a message box with "Object Windows Exception", "GDI Failure." I click OK and I then get several dozen page faults and Borland closes. (I had posted this in the turbo forum, thinking it was my compiler but didn''t get much response so I''m sorry since I know people don''t like multiple posts of the same thing. Anyway,) For code, I''m basically using the base file from the end of Chapter 4. The error appears if all I do is create one brush and pen and try to draw a rectangle. Basically, I am wondering what this error is and if its my compiler or if this could happen on some other compiler, be it VC++ or some other one. The chapter deals with mainly drawing pixels, lines, polygons, ellipses, and rectangles using GDI from Windows. Below are the main lines that I''m using to do the drawing, taken pretty much word for word from the book. I simply changed some of the values. hdc = GetDC(hwnd); red_pen = CreatePen(PS_SOLID, 1, RGB(255, 0, 0)); black_brush = CreateSolidBrush(RGB(255, 255, 255)); SelectObject(hdc, red_pen); SelectObject(hdc, black_brush); Rectangle(hdc, 0, 0, 133, 133); ReleaseDC(hwnd, hdc);

Share this post


Link to post
Share on other sites
LessBread    1415
You shouldn''t release the DC while you have objects selected into it. Try this instead:

HPEN oldpen = SelectObject(hdc, red_pen);
HBRUSH oldbrush = SelectObject(hdc, black_brush);

Rectangle(hdc, 0, 0, 133, 133);

SelectObject(hdc, oldpen);
SelectObject(hdc, oldbrush);

ReleaseDC(hwnd,hdc);


Share this post


Link to post
Share on other sites