Which is a better practice? (Global handle or finding handle)

This is a discussion on Which is a better practice? (Global handle or finding handle) within the Windows Programming forums, part of the Platform Specific Boards category; For a rather large program, with probably close to a hundred controls or maybe even more: Would it be more ...

  1. #1
    Grey Wizard C_Sparky's Avatar
    Join Date
    Sep 2009
    Posts
    50

    Which is a better practice? (Global handle or finding handle)

    For a rather large program, with probably close to a hundred controls or maybe even more:
    Would it be more practical to just store a global HWND variable for the main window, or to call FindWindow() each time the main window handle is needed to retrieve the handles of child controls?

    It's sort a decision between memory and processing, but the difference is so small it's impossible to notice on modern machines. So I guess it wouldn't make too much of a difference. Just wondering what some experienced software engineers/developers thought.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    I can't claim the "engineer" label but I have devloped a bit of windows stuff...

    What I do when working outside of dialogs is make an array for all my window handles.

    Code:
    HWND Wind[23];  // for 23 windows & controls
    
    Wind[0] = CreateWindowEx(...
    If I need access to them in more than one source page, I'll place it in a header file. Although commonly it's only the main window I need to access that way so I will make a copy in the header... MainWind = Wind[0];

    From there I simply refer to them by number...
    For example...
    Code:
    SetWindowText(Wind[12],"Hello There");
    I don't think it makes a huge difference, but I find it very convenient for things like changing fonts or mass clearing of data... A simple for loop and off you go.
    Last edited by CommonTater; 12-30-2010 at 03:24 AM.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    681
    If it's C++, I would create a separate class for this window, as well as for the other controls. It would probably lead to creating own GUI library...

    Or if the GUI was nicely and completely separated from the core, making global HWND probably wouldn't harm much the code (only the GUI in the worst case).
    I never put signature, but I decided to make an exception.

  4. #4
    Grey Wizard C_Sparky's Avatar
    Join Date
    Sep 2009
    Posts
    50
    Thanks for the suggestions.

    I'll just make a single global HWND variable, it only takes up 4 bytes

    And GetDlgItem() is incredibly useful to this case

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    I usually use GetParent() to find the windows/controls parent's/owner's HWND (and avoid globals if possible).
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding window handle again (pic)
    By freedik in forum Windows Programming
    Replies: 0
    Last Post: 08-20-2003, 04:53 AM
  2. Is there any way?
    By Blackbox in forum C++ Programming
    Replies: 2
    Last Post: 05-21-2003, 06:05 PM
  3. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM
  4. a simple C question...
    By DramaKing in forum C Programming
    Replies: 10
    Last Post: 07-28-2002, 02:04 PM
  5. Finding the handle of an open window..
    By ExDigit in forum Windows Programming
    Replies: 1
    Last Post: 01-04-2002, 04:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21