Sign in to follow this  

Why are hwnd, hDC etc not Global?

This topic is 3319 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

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 this post


Link to post
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 this post


Link to post
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

Share this post


Link to post
Share on other sites

This topic is 3319 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this