Search code examples
c++winapiorganizationhwndwinmain

WinAPI - how to organize lots of HWND objects?


As I push forward my first winapi UI, I find myself creating large, uncomfortable stacks of HWND variables in my WinMain file:

HWND foo;
HWND bar;
HWND baz;
HWND etc;

int WINAPI WinMain(...) {}

Of course, these variables are used in the functions in the rest of the file - the message loop, for example - so they must be accessible.

For my relatively small UI, I'll have something like 30 HWNDs piled up so they're in a visible scope. This makes me very suspicious that I'm doing it wrong.

Is this what should be happening, or is there a more practical way to organize this?


Solution

  • You have a few solutions, depending what your program is.

    1. You can put all those handles in one or many containers like std::vector.
    2. You can map them like chris suggests.
    3. If your program gets big, you might want to organize them into logical units. For example, if 15 of those windows are for one half of your logic and the other 15 for another half (say controls inside tabs) then you might want to group those controls in some way (file, class, whatever is the most logical fit).