问题
Lets say I have an MFC Dialog with several buttons on it. (E.g. "Red", "Blue", "Green", and "Yellow" buttons) These buttons all have IDs such as ("IDC_BUTT_RED","IDC_BUTT_BLUE","IDC_BUTT_GREEN","IDC_BUTT_YELLOW")
Given that I have a CWnd object that references the dialog window that these buttons are placed on. Is there a way to get a list of these IDs?
I know there is a CWnd::GetNextDlgGroupItem
method, that based on the description should iterate through a group of controls. I tried using it, passing in a CWnd object referring to one of the buttons, then calling the GetWindowText
method to check, but it always returns a reference to itself.
回答1:
You can enumerate all of the dialog's child windows using EnumChildWindows. This is a C API function so you use and get HWNDs instead of CWnds. When each child window's HWND is passed to your callback function you can call GetClassName to find its window class name (like "BUTTON") and GetWindowLong to find its ID.
来源:https://stackoverflow.com/questions/27788886/is-there-a-way-to-get-a-reference-to-all-the-child-windows-or-controls-on-an-mfc