window I've created stop working

Hi guys, I'm a new user and not sure if this question should go in the begginers sections :S

My problem is that if I resize a window I've created and hold down the left mouse button for 5-6 seconds while resizing, then the window completely stop working, it stops refreshing, the frame becomes a solid color and the minimize,maximize,close buttons all disappear.

Can anyone tell me what am I doing wrong in my code? (code below, had to put into pastebin since was too long and the forum didn't allow me to paste it here)

http://pastebin.com/NKE0FJHC
Last edited on
Don't know if this could help to help me, but I've pin-pointed the cause of the bug to my DrawPoint() function call on line 223, turning it off it doesn't bug my window out.

But how's that function breaking my window?! Does anybody have an explanation for it? :\

The bug is in one of the commented out lines,since it doesn't accur without them.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void DrawPoint(HDC hdc, int x, int y, int size, COLORREF color)
{
	//LOGBRUSH lb{};
	//lb.lbColor = color;
	//lb.lbStyle = BS_SOLID;
	//lb.lbHatch = HS_BDIAGONAL;
	//HBRUSH fill = CreateBrushIndirect(&lb);

	//HGDIOBJ previousFill = SelectObject(hdc, fill);

	int left = x - size / 2;
	int right = x + size / 2;
	int top = y - size / 2;
	int bottom = y + size / 2;
	Ellipse(hdc, left, top, right, bottom);

	//SelectObject(hdc, previousFill);
}


Edit : well, I turned on and off lines one at the time, the problem was in the "HBRUSH fill = CreateBrushIndirect(&lb);" and checking documentation, it says:

When you no longer need the brush, call the DeleteObject function to delete it.

...I wasn't doing that, is it a big deal?! xD (well apparently yes, since by deleting it my windows now doesn't break anymore... :S)
Last edited on
Yea, its a big deal. The overarching concern is that its a memory/resource leak. That's why Microsoft created .NET. It isn't that .NET is better than Win Api; its just that it protects folks who don't know what they are doing from shooting themselves in the foot. My guess is that you'll be more careful from now on. Any GDI call which returns an 'object' to you must release the 'object'. That would include HBRUSHs, HFONTs, HDCs, etc.
I will be more careful, and hopeful I will be even more for when I reach the end of Programming Windows API 5th, but in the meantime, more dangerous experiments need to be performed, for science! xD (meaning you'll see me posting here again soon :P)
Last edited on
If you managed to get through that whole book you're a better man than me! :) I only ever managed the first few chapters, then used the rest for reference when I needed it (which was a lot).
Haha :D
The question is not if I will get trough it, the question is "when" xD
Because every 5 pages I read, it presents me some new tools that evolve in entire detours on my part, like I read today around page 137 about PolyBezier() and then the rest of the day was gone before I realized it making my bezier editor with handles that change colors when click and stuff, for no reason at all (http://i.imgur.com/mOaw8bP.png ) xD


Though I can tell you for sure I will skip or read really fast trough the Printer chapter, can't really care a bit about that, don't even have a printer ... :S
Last edited on

Though I can tell you for sure I will skip or read really fast trough the Printer chapter, can't really care a bit about that, don't even have a printer ... :S


It took me about 15 years before I needed that printer chapter. About a year ago I needed it. I have both Petzold's Windows 95 book and the last edition which you likely have, which came out about the time of Windows 98/2000. His code from both books worked like a charm!
Yeah I noticed that too with other examples code from the book, which makes me wonder if he wrote that book from the future xD
Or maybe Windows API hasn't changed that much over time (I yet don't know)
Last edited on

Or maybe Windows API hasn't changed that much over time (I yet don't know)


It doesn't change much, as its the lowest level abstraction to the operating system. In that sense it has always been a good investment of time to learn. In my case I strictly do desktop Windows coding, so it works for me. You seem to be interested in graphics, and there have been quite a lot of changes to that. GDI still works, but most folks nowadays use DirectX or piles of other technologies.
Topic archived. No new replies allowed.