Discussion:
How do you obtain a Window Handle?
(too old to reply)
Charlie Chan
2008-02-04 13:06:07 UTC
Permalink
I am a beginner and having a very difficult time with some tasks.
Does anyone have a COMPLETE example of how to obtain the handle of a
Child window in VC++ 6? All the examples I have found contain
incomplete information. It is as-if one needed to know the handle to
the window to obtain the handlt to the window. I've tried ever
process I can find in my MSDN Library and they all seen to compare
with a cat chasing it's own tail.
Check Abdoul
2008-02-04 13:41:29 UTC
Permalink
Take a look at the CWnd::GetWindow() member function. You should be able to
pass GW_CHILD and then GW_NEXT in a loop to get all the child windows of a
given main window. If you want to do it without MFC, then use the
GetWindow() API. Something like


TCHAR sClassName[MAX_PATH+1] = {0};

HWND hWndChild = ::GetWindow( hWndParent, GW_CHILD);
while ( hWndChild != NULL )
{
// Do something with the hWndChild. For example call
// GetClassName(hWndChild, sClassName, MAX_PATH );
// to get its window class name

hWndChild = ::GetWindow(hWndChild, GW_HWNDNEXT);
}


Cheers
Check Abdoul
---------------------
Post by Charlie Chan
I am a beginner and having a very difficult time with some tasks.
Does anyone have a COMPLETE example of how to obtain the handle of a
Child window in VC++ 6? All the examples I have found contain
incomplete information. It is as-if one needed to know the handle to
the window to obtain the handlt to the window. I've tried ever
process I can find in my MSDN Library and they all seen to compare
with a cat chasing it's own tail.
David Ching
2008-02-04 16:45:07 UTC
Permalink
Post by Check Abdoul
Take a look at the CWnd::GetWindow() member function. You should be able
to pass GW_CHILD and then GW_NEXT in a loop to get all the child windows
of a given main window. If you want to do it without MFC, then use the
GetWindow() API. Something like
TCHAR sClassName[MAX_PATH+1] = {0};
HWND hWndChild = ::GetWindow( hWndParent, GW_CHILD);
while ( hWndChild != NULL )
{
// Do something with the hWndChild. For example call
// GetClassName(hWndChild, sClassName, MAX_PATH );
// to get its window class name
hWndChild = ::GetWindow(hWndChild, GW_HWNDNEXT);
}
You can also use EnumChildWindows() instead of the GetWindow() in loop,
which is safer. MS recommends you don't use GetWindow() in a loop because
if a window is created or destroyed while you are looping, you could hang.

In addition to EnumChildWindows(), you can also use FindWindowEx() to find a
child window with a classname or widow caption. This might actually be more
what OP wants.

-- David
Ajay Kalra
2008-02-04 18:19:42 UTC
Permalink
Post by David Ching
Post by Check Abdoul
Take a look at the CWnd::GetWindow() member function. You should be able
to pass GW_CHILD and then GW_NEXT in a loop to get all the child windows
of a given main window. If you want to do it without MFC, then use the
GetWindow() API. Something like
TCHAR sClassName[MAX_PATH+1] = {0};
HWND hWndChild = ::GetWindow( hWndParent, GW_CHILD);
while ( hWndChild != NULL )
{
// Do something with the hWndChild. For example call
// GetClassName(hWndChild, sClassName, MAX_PATH );
// to get its window class name
hWndChild = ::GetWindow(hWndChild, GW_HWNDNEXT);
}
You can also use EnumChildWindows() instead of the GetWindow() in loop,
which is safer. MS recommends you don't use GetWindow() in a loop because
if a window is created or destroyed while you are looping, you could hang.
In addition to EnumChildWindows(), you can also use FindWindowEx() to find a
child window with a classname or widow caption. This might actually be more
what OP wants.
To me it appeared that OP really wants to enumerate child windows.
Dunno.

---
Ajay
Scott McPhillips [MVP]
2008-02-04 18:37:26 UTC
Permalink
Post by Ajay Kalra
Post by David Ching
In addition to EnumChildWindows(), you can also use FindWindowEx() to find a
child window with a classname or widow caption. This might actually be more
what OP wants.
To me it appeared that OP really wants to enumerate child windows.
Dunno.
As long as we are guessing, my guess would be that OP created the child
window, so should have a pointer or handle available from that operation.

To OP: Did you create the child window? If so show us that code. It should
give you want you need.
--
Scott McPhillips [VC++ MVP]
Ajay Kalra
2008-02-04 19:36:48 UTC
Permalink
Post by Scott McPhillips [MVP]
As long as we are guessing, my guess would be that OP created the child
window, so should have a pointer or handle available from that operation.
That was my first shot. I assumed that OP had a CWnd and OP needed was
m_hWnd. Dunno.

---
Ajay
Check Abdoul
2008-02-05 02:55:08 UTC
Permalink
Hi David,
Post by David Ching
You can also use EnumChildWindows() instead of the GetWindow() in loop,
which is safer.
I agree that you can also use EnumChildWindows() API. However I think as
far as the implementation of both of these API's goes, they might probably
be returning the information from some internal windows list.
Post by David Ching
MS recommends you don't use GetWindow() in a loop because if a window is
created or destroyed while you are looping, you could hang.
I was using the example to get the child windows of a main window. Not
the child windows of the Desktop. So unless an application creates dynamic
child windows within very short time intervals, the probablity of
GetWindow() failing/hanging in this scenario is very small.

Cheers
Check Abdoul
---------------------
Post by David Ching
Take a look at the CWnd::GetWindow() member function. You should be able
to pass GW_CHILD and then GW_NEXT in a loop to get all the child windows
of a given main window. If you want to do it without MFC, then use the
GetWindow() API. Something like
TCHAR sClassName[MAX_PATH+1] = {0};
HWND hWndChild = ::GetWindow( hWndParent, GW_CHILD);
while ( hWndChild != NULL )
{
// Do something with the hWndChild. For example call
// GetClassName(hWndChild, sClassName, MAX_PATH );
// to get its window class name
hWndChild = ::GetWindow(hWndChild, GW_HWNDNEXT);
}
You can also use EnumChildWindows() instead of the GetWindow() in loop,
which is safer. MS recommends you don't use GetWindow() in a loop because
if a window is created or destroyed while you are looping, you could hang.
In addition to EnumChildWindows(), you can also use FindWindowEx() to find
a child window with a classname or widow caption. This might actually be
more what OP wants.
-- David
Check Abdoul
2008-02-05 02:55:55 UTC
Permalink
Hi David,
Post by David Ching
You can also use EnumChildWindows() instead of the GetWindow() in loop,
which is safer.
I agree that you can also use EnumChildWindows() API. However I think as
far as the implementation of both of these API's goes, they might probably
be returning the information from some internal windows list.
Post by David Ching
MS recommends you don't use GetWindow() in a loop because if a window is
created or destroyed while you are looping, you could hang.
I was using the example to get the child windows of a main window. Not
the child windows of the Desktop. So unless an application creates dynamic
child windows within very short time intervals, the probablity of
GetWindow() failing/hanging in this scenario is very small.

Cheers
Check Abdoul
---------------------
Post by David Ching
Take a look at the CWnd::GetWindow() member function. You should be able
to pass GW_CHILD and then GW_NEXT in a loop to get all the child windows
of a given main window. If you want to do it without MFC, then use the
GetWindow() API. Something like
TCHAR sClassName[MAX_PATH+1] = {0};
HWND hWndChild = ::GetWindow( hWndParent, GW_CHILD);
while ( hWndChild != NULL )
{
// Do something with the hWndChild. For example call
// GetClassName(hWndChild, sClassName, MAX_PATH );
// to get its window class name
hWndChild = ::GetWindow(hWndChild, GW_HWNDNEXT);
}
You can also use EnumChildWindows() instead of the GetWindow() in loop,
which is safer. MS recommends you don't use GetWindow() in a loop because
if a window is created or destroyed while you are looping, you could hang.
In addition to EnumChildWindows(), you can also use FindWindowEx() to find
a child window with a classname or widow caption. This might actually be
more what OP wants.
-- David
Hans-J. Ude
2008-02-05 01:36:38 UTC
Permalink
Post by Check Abdoul
Take a look at the CWnd::GetWindow() member function. You should be able to
pass GW_CHILD and then GW_NEXT in a loop to get all the child windows of a
given main window. If you want to do it without MFC, then use the
GetWindow() API. Something like
TCHAR sClassName[MAX_PATH+1] = {0};
HWND hWndChild = ::GetWindow( hWndParent, GW_CHILD);
while ( hWndChild != NULL )
{
// Do something with the hWndChild. For example call
// GetClassName(hWndChild, sClassName, MAX_PATH );
// to get its window class name
hWndChild = ::GetWindow(hWndChild, GW_HWNDNEXT);
}
I once wrote some code based on that method. It's recursive and
displays all windows in the system in a CTreeCtrl. Just like Spy++.
Since results are sometimes slightly different I'm wondering what's
buggy. Spy++ or my code. I guess Spy++ ;)

Hans

////////// Top of file ////////////////////
void CWindowDlg::PopulateWinTree()
{
CString str;
TVINSERTSTRUCT tvi;

c_winTree.DeleteAllItems();
HWND hwnd = ::GetDesktopWindow();
str = FormatEntry(hwnd);
str += _T("(Desktop)");

tvi.hParent = TVI_ROOT;
tvi.hInsertAfter = TVI_ROOT;
tvi.item.mask = TVIF_PARAM | TVIF_TEXT;
tvi.item.lParam = (LPARAM) hwnd;
tvi.item.pszText = (LPTSTR) LPCTSTR(str);
HTREEITEM hRoot = c_winTree.InsertItem (&tvi);

hwnd = ::GetWindow(::GetDesktopWindow(), GW_CHILD);
RecurseWindows(hwnd, hRoot);
c_winTree.Expand(hRoot, TVE_EXPAND);
}

HTREEITEM CWindowDlg::RecurseWindows(HWND hwnd, HTREEITEM hParent)
{
TVINSERTSTRUCT tvi;
HTREEITEM hTree = 0;
CString str;

if (hwnd == 0)
return 0;

hwnd = ::GetWindow(hwnd, GW_HWNDFIRST);
tvi.hParent = hParent;
tvi.hInsertAfter = TVI_LAST;
tvi.item.mask = TVIF_PARAM | TVIF_TEXT ;

while (hwnd)
{
if (::GetWindowLong(hwnd, GWL_STYLE) & WS_VISIBLE)
{
// Visible Window
}
else
{
// Hidden Window
}
str = FormatEntry(hwnd);
tvi.item.pszText = (LPTSTR) LPCTSTR(str);
tvi.item.lParam = (LPARAM) hwnd;
hTree = c_winTree.InsertItem (&tvi);
c_winTree.SetItem (&tvi.item);

RecurseWindows(::GetWindow(hwnd, GW_CHILD), hTree);
hwnd = ::GetWindow(hwnd, GW_HWNDNEXT);
}
return hTree;
}

CString& CWindowDlg::FormatEntry(HWND hwnd)
{
static CString str;
TCHAR wndtext[256];
TCHAR classname[256];

wndtext[0] = classname[0] = _T('\0');
::GetWindowText(hwnd, wndtext, sizeof(wndtext)/sizeof(TCHAR));
::GetClassName(hwnd, classname,
sizeof(classname)/sizeof(TCHAR));
str.Format (_T("%08Xh \"%s\" \'%s\' "), hwnd, wndtext,
classname);
return str;
}

void CWindowDlg::OnRefresh()
{
PopulateWinTree();
}
////////// Bottom of file ////////////////////
Ajay Kalra
2008-02-04 14:18:24 UTC
Permalink
Post by Charlie Chan
I am a beginner and having a very difficult time with some tasks.
Does anyone have a COMPLETE example of how to obtain the handle of a
Child window in VC++ 6? All the examples I have found contain
incomplete information. It is as-if one needed to know the handle to
the window to obtain the handlt to the window. I've tried ever
process I can find in my MSDN Library and they all seen to compare
with a cat chasing it's own tail.
In addition to what Check mentioned, if you already have a CWnd, you
can get the handle from m_hWnd member. Are you asking to enumerate
child windows?

---
Ajay
Charlie Chan
2008-02-05 01:40:38 UTC
Permalink
Post by Charlie Chan
I am a beginner and having a very difficult time with some tasks.
Does anyone have a COMPLETE example of how to obtain the handle of a
Child window in VC++ 6?  All the examples I have found contain
incomplete information.  It is as-if one needed to know the handle to
the window to obtain the handlt to the window.  I've tried ever
process I can find in my MSDN Library and they all seen to compare
with a cat chasing it's own tail.
The program is being created using the VC++ 6 IDE. It is a Multi-
Document Interface (MDI) program. The Child window, as are all
windows, ia created using the IDE provided code. The Child appears on
the inside of the Main Frame. It is what I need the handle to. The
contents of the Child window is text loaded from an external text
file. It is a simple learning program that I created to help me
develope a better understanding of VC++ 6.

CChildFrame
IMPLEMENT_DYNCREATE(CChildFrame, CMDIChildWnd)

BEGIN_MESSAGE_MAP(CChildFrame, CMDIChildWnd)
//{{AFX_MSG_MAP(CChildFrame)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code !
//}}AFX_MSG_MAP
END_MESSAGE_MAP( )
David Ching
2008-02-05 05:22:47 UTC
Permalink
Post by Charlie Chan
The program is being created using the VC++ 6 IDE. It is a Multi-
Document Interface (MDI) program. The Child window, as are all
windows, ia created using the IDE provided code. The Child appears on
the inside of the Main Frame. It is what I need the handle to. The
contents of the Child window is text loaded from an external text
file. It is a simple learning program that I created to help me
develope a better understanding of VC++ 6.
CChildFrame
IMPLEMENT_DYNCREATE(CChildFrame, CMDIChildWnd)
BEGIN_MESSAGE_MAP(CChildFrame, CMDIChildWnd)
//{{AFX_MSG_MAP(CChildFrame)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code !
//}}AFX_MSG_MAP
END_MESSAGE_MAP( )
You can use Spy++ to find out the classname/window caption of these windows
so you know what you are looking for when you enumerate them.

-- David
Ajay Kalra
2008-02-05 15:48:21 UTC
Permalink
Post by Charlie Chan
Post by Charlie Chan
I am a beginner and having a very difficult time with some tasks.
Does anyone have a COMPLETE example of how to obtain the handle of a
Child window in VC++ 6? All the examples I have found contain
incomplete information. It is as-if one needed to know the handle to
the window to obtain the handlt to the window. I've tried ever
process I can find in my MSDN Library and they all seen to compare
with a cat chasing it's own tail.
The program is being created using the VC++ 6 IDE. It is a Multi-
Document Interface (MDI) program. The Child window, as are all
windows, ia created using the IDE provided code. The Child appears on
the inside of the Main Frame. It is what I need the handle to. The
contents of the Child window is text loaded from an external text
file. It is a simple learning program that I created to help me
develope a better understanding of VC++ 6.
CChildFrame
IMPLEMENT_DYNCREATE(CChildFrame, CMDIChildWnd)
BEGIN_MESSAGE_MAP(CChildFrame, CMDIChildWnd)
//{{AFX_MSG_MAP(CChildFrame)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code !
//}}AFX_MSG_MAP
END_MESSAGE_MAP( )
If all you want is the childwindowframes, you can simply enumerate all
the views from the document and get the parent frame(which is the
child frame). You can always get the active frame from mainframe by
using
GetActiveFrame on it.

---
Ajay

Continue reading on narkive:
Loading...