cranbo
asked on
Getting all controls in a dialog
Is there any way I can get a list of all the controls I place on a CDialog so that I can iterate through all of them and perform some action on them?
Trying using EnumChildControls.
The function uses a callback function which is called once for each control that's enumerated.
- return FALSE in the callback if you're finished enumerating controls
- return TRUE to continue on to the next control
- place the callback in your dialog's .cpp file.
You can get the type of control by using the GetClassName function.
The following is an example on how to change all button text in a dialog to "What?"
BOOL CALLBACK EnumChildControls(HWND hWnd, LPARAM lParam)
{
// Make sure window is valid
if (hWnd && IsWindow(hWnd))
{
CWnd* pWnd = CWnd::FromHandle(hWnd);
ASSERT_VALID(pWnd);
// Get the class name for the window
TCHAR szClassName[100];
GetClassName(hWnd, szClassName, 100);
TRACE(_T("Class Name: %s\n"), szClassName);
// Change text on all buttons to What?
if (_tcscmp(_T("Button"), szClassName) == 0)
{
CButton* pButton = (CButton*)pWnd;
pButton->SetWindowText(_T( "What?"));
}
}
return TRUE;
}
void CTestDlgDlg::OnButton2()
{
// Enumerate the controls
EnumChildWindows(GetSafeHw nd(), EnumChildControls, 0);
}
The function uses a callback function which is called once for each control that's enumerated.
- return FALSE in the callback if you're finished enumerating controls
- return TRUE to continue on to the next control
- place the callback in your dialog's .cpp file.
You can get the type of control by using the GetClassName function.
The following is an example on how to change all button text in a dialog to "What?"
BOOL CALLBACK EnumChildControls(HWND hWnd, LPARAM lParam)
{
// Make sure window is valid
if (hWnd && IsWindow(hWnd))
{
CWnd* pWnd = CWnd::FromHandle(hWnd);
ASSERT_VALID(pWnd);
// Get the class name for the window
TCHAR szClassName[100];
GetClassName(hWnd, szClassName, 100);
TRACE(_T("Class Name: %s\n"), szClassName);
// Change text on all buttons to What?
if (_tcscmp(_T("Button"), szClassName) == 0)
{
CButton* pButton = (CButton*)pWnd;
pButton->SetWindowText(_T(
}
}
return TRUE;
}
void CTestDlgDlg::OnButton2()
{
// Enumerate the controls
EnumChildWindows(GetSafeHw
}
Sorry, in the first line of the last message I meant EnumChildWindows, not EnumChildControls. EnumChildControls is the name of the callback function in my code.
The code is correct.
The code is correct.
Oops! Sorry about forgetting about using GetClassName to get the control type in ::EnumChildWindows callback function.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I accepted nik2k as all I want to do is enable/disable the controls so I only need the CWnd* for each and the answer is very concise. Perhaps I should have explained that.. Anyway, many thanks to all!
Another solution is to use the API function ::EnumChildWindows. You will need to specify a procedure to receive the handles to these child windows(the controls). The problem with this method is that you do not know what type of control is returned. See API documentation for further info.
Hope this helps.