Discussion:
Different results from EnumChildWindows vs FindWindowEx
(too old to reply)
Chris J
17 years ago
Permalink
Hi-

I'm relatively new to the win32 API realm. I've been using the API to try
and find controls within a top-level window. I tried using FindWindowEx to
find the "Exit" button from the window, but it always returns NULL. I finally
set up a call to EnumChildWindows, and I can find the control that way.

Are there conditions under which these two methods are expected to return
different results like this? The window that I'm getting information about is
a .NET application.

Thanks.
Alex Blekhman
17 years ago
Permalink
I've been using the API to try and find controls within a
top-level window. I tried using FindWindowEx to find the "Exit"
button from the window, but it always returns NULL. I finally
set up a call to EnumChildWindows, and I can find the control
that way.
Could you post some code, please? Also, ensure that you give
correct parent window handle to `FindWindowEx' functon.

Alex
Chris J
17 years ago
Permalink
I'm actually writing GUI testing code in Ruby, using a Win32 API module that
wraps actual API calls. However, I reproduced the same issue in C++. The C++
code appends output to a list box in a GUI:

BOOL CALLBACK EnumChildProc(HWND hwndChild, LPARAM lParam)
{
HWND hParent = (HWND)lParam;

int txtLen;

int len = GetWindowTextLength(hwndChild);
if(len > 0)
{
char* buf;

buf = (char*)GlobalAlloc(GPTR, len + 1);
GetWindowText(hwndChild, buf, len + 1);

if (strlen(buf) > 0) {
int index = SendDlgItemMessage(hParent, IDC_MAIN_LIST, LB_ADDSTRING,
0, (LPARAM)buf);
}

GlobalFree((HANDLE)buf);
}
return TRUE;
}


Here's the code that calls EnumChildWindows and FindWindowEx

HWND hTopWindow;
HWND hExitBtn;
char* message;
// Find my application's window and enumerate its children.
hTopWindow = FindWindow(NULL,"My Application");

if (hTopWindow > 0) {
EnumChildWindows( hTopWindow, EnumChildProc, hwnd );
}

// Now try to find the Exit button explicitly.
hExitBtn = FindWindowEx(hTopWindow, 0, NULL, "e&xit");
message = (hExitBtn > 0) ? "Exit button was found." :
"Exit button NOT found.";

SendDlgItemMessage(hwnd, IDC_MAIN_LIST, LB_ADDSTRING, 0,
(LPARAM)message);


EnumChildProc displays "E&xit" as one of the child control's text, but
FindWindowEx does not find the control.
Post by Alex Blekhman
Could you post some code, please? Also, ensure that you give
correct parent window handle to `FindWindowEx' functon.
Alex
Norman Bullen
17 years ago
Permalink
...
Perhaps FindWindowEx() did not find the window because "e&xit" does not
match "E&xit".
--
Norm

To reply, change domain to an adult feline.
Chris J
17 years ago
Permalink
According the documentation for FindWindowEx, "This function does not
perform a case-sensitive search.". Just in case (no pun intended) I have
also tried "E&xit" and "Exit", to no avail.
Post by Norman Bullen
Perhaps FindWindowEx() did not find the window because "e&xit" does not
match "E&xit".
--
Norm
To reply, change domain to an adult feline.
Alexander Grigoriev
17 years ago
Permalink
Go to the documentation for FindWindowEx. Find a note about GetWindowText.
Go to GetWindowText documentation.
...
Chris J
17 years ago
Permalink
Thanks for the documentation pointer. That may be what is going on. In the
Ruby code that I have, it actually starts the application process that
displays the window for which I am trying to locate the Exit button. Given
that design, I would expect that the process owns the target window, but
perhaps in that environment it doesn't.
...
Loading...