Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Child Window

Status
Not open for further replies.

moonoo

Programmer
May 17, 2000
62
US
Hi,
From the handle of the parent window how do i get the
handles of all child windows.
Please Help
 
Hi

Use the function EnumChildWindows. This function uses a callback. I already explained this a few threads ago concerning an other function that uses callback.

I test the following code in a dialog bow where I put a button called IDC_GET and several other controls. The OnGet() is the handler of this button.

Here it is:

void CEnumChildDlg::OnGet()
{

HWND hwnd = this->GetSafeHwnd();

if ( !EnumChildWindows( hwnd, EnumChildProc, ( LPARAM) hwnd))
AfxMessageBox( "Problem getting Child Windows");
}
The first parameter is the handle of the parent window. The third parameter is the parameter that is passed to the callback function. In this case, this is the same handle. It could be used to transfer data back to the calling process. Remember that a static function accesses only static members, so passing the handle of the calling process is a mean to exchange data between a static and a non-static member. See the code below

Remember thet a callback must be declared static.
As this:

public:
static BOOL CALLBACK EnumChildProc( HWND hwnd, LPARAM lParam);

In this simple example, I'll just display the control id in the debug window ...

BOOL CALLBACK CEnumChildDlg::EnumChildProc( HWND hwnd, LPARAM lParam)
{
// Get a Pointer to the window found by EnumChildWindows function (first param)

CWnd* pWnd = CWnd::FromHandle( hwnd);
CString str;

if ( pWnd != ( CWnd*) NULL) // Safety Check
{
str.Format( "Child Id %d \n", pWnd->GetDlgCtrlID());

::OutputDebugString( str);

return TRUE;
}

return FALSE;
}

If you want to update a member in the calling class, you can do it like this:

I'll assume here that the calling class is CEnumChildDlg.

CEnumChildDlg* pParentWnd =
( CEnumChildDlg* ) CWnd::FromHandle( ( HWND) lParam);

// Update Member of CEnumChildDlg class

pParentWnd-> .... = pWnd->GetDlgCtrlID();

In this case you update a memmber of the class CEnumChildDlg from within the callback function. Of course, if you need to transfer the id of all child windows, you'll use an array or a list...

HTH

Thierry
EMail: Thierry.Marneffe@swing.be

 
Hi

I remember that I also used an other way to do it. To be honnest, I didn't check if both give the same result. It should but I don't know.

As you can see, it much more simple:

CWnd* pWnd = GetWindow( GW_CHILD);

while( pWnd)
{
if ( pWnd->GetDlgCtrlID() > 0)
{
// Perform Specific processing here

.........

pWnd = pWnd->GetWindow( GW_HWNDNEXT);
}
}

HTH

Thierry
EMail: Thierry.Marneffe@swing.be

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top