Why are hwnd, hDC etc not Global?

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

Recommended Posts

Hi guys, I have been writing my own windows application template, and I am just wondering why I never see all the handels (hwnd, hDC and hRC etc) defined globably, usually multiple variable for the same handel define localy in each function i.e WinMain and the Procedure functions. I also notice that the HWND handel in the WinMain function is not used in the its Windows Procedure function, it uses usaully the hWnd parameter of itself, which sort of leads me to think that they are different? which cant be true or the program would be totaly mucked up.
WinMain(...)
{
HWND ghWnd;
HDC ghDC;
...
ghDC = GetDC(ghWnd);
...
}

WinProc(HWND hWnd ...)
{
static HDC hDC;
...
switch(...)
{
case WM_CREATE:
hDC = GetDC(hWnd) // using the local hWnd handel
// but surly there is no difference between
// hWnd and ghWnd so why not define globably?
...
}
}

I would love to understand the reason for this, if anyone could explain that or point me in the direction of some good articles furthering, thanks guys :D

Share on other sites
The same window procedure may be used for several windows. Therefore, in order to determine which window received the message, the handle of that window is passed as an argument to the window procedure.

If you only have one window, then that argument will always be the same (and will be equal to the handle that you obtained when you created that window).

Share on other sites
It's also good programming practice... don't use globals unless there is no other way...

Share on other sites
Ah oh k thanks ToohrVyk, didnt consider that, makes perfect sense, and yes larvyde I will remember that, I suppose where I am at it would make much of a difference, but I can see in large programs it could be very easy to change a global accidently :-|, thanks guys :D

1. 1
Rutin
24
2. 2
JoeJ
20
3. 3
4. 4
5. 5

• 9
• 46
• 41
• 23
• 13
• Forum Statistics

• Total Topics
631749
• Total Posts
3002040
×