c++clrhwndfindwindowfindwindowex

How to get the list of controls from the window in Visual C++?


How to get the list of the controls from the window, specified by "FindWindow" function? For example I have a handle to the Notepad window by

HWND Window = FindWindow(L"Notepad", L"dummy.txt - Notepad");

then I can make a handle to an "Edit" control by

HWND WindowEX = FindWindowEx(Window, NULL, L"EDIT", NULL);

but how can I get a full list of the controls and record them in the array for example?


Solution

  • I've done it the old way

    array<HWND>^ childs = gcnew array<HWND>(1000);
    array<String^>^ childsnames = gcnew array<String^>(1000);
    int childcount = 0;
    public:
        delegate bool EnumDelegate(HWND hWnd, int lParam);
    public:
        [DllImport("user32.dll", EntryPoint = "EnumChildWindows", ExactSpelling = false, CharSet = CharSet::Unicode, SetLastError = true)]
        static bool EnumChildWindows(HWND hwndParent, EnumDelegate ^lpEnumCallbackFunction, int lParam);
    
    bool enumchildproc(HWND hWnd, int lParam)
        {
            int nLength = GetWindowTextLength(hWnd);
            LPWSTR strbTitle = (LPWSTR)VirtualAlloc((LPVOID)NULL,
                (DWORD)(nLength + 1), MEM_COMMIT,
                PAGE_READWRITE);
            //GetWindowText(hWnd, strbTitle, 255);
            GetClassNameW(hWnd, strbTitle, 255);
            String ^strTitle = gcnew String(strbTitle);
            childs[childcount] = hWnd;
            childsnames[childcount] = strTitle;
            childcount++;
            return true;
        }
    
    void refreshChilds(HWND Parent, ComboBox^ cb)
        {
            cb->Items->Clear();
            childcount = 0;
            int i = 0;
            Array::Clear(childs, 0, 1000);
            Array::Clear(childsnames, 0, 1000);
            EnumDelegate ^filter = gcnew EnumDelegate(this, &MyForm::enumchildproc);
            EnumChildWindows(Parent, filter, 0);
            for (i = 0; i < childcount; i++)
            {
                cb->Items->Add(childsnames[i]);
            }
        }
    

    refreshChilds() function will fill the combobox with childs classnames.