Enumerating Desktop Windows

Started by
5 comments, last by ApochPiQ 17 years, 7 months ago
Hey All, I'm using C++ and EnumWindows to attempt to get the handles of all of my open programs. Unfortunately I can't seem to find the perfect combination of tests to get only the windows for open programs. I'm either getting too many or not enough. I'm using a combination of:

if (!IsWindowVisible(hWnd))
	return true;

if (!GetParent(hWnd) == 0)
	return true;

if (!GetWindow(hWnd, GW_OWNER) == 0)
	return true;

to only grab the windows that are programs (I read somewhere that this was one way of doing this). If the window fails all of these tests, then it should be a program window and the function will continue. For some reason though, I'm not getting windows that are minimized, or even obstructed by other programs unless I bring them to the front. I was under the impression that the IsWindowVisible() function only checks to make sure that a window has the VISIBLE flag and not that it was actually visible. Any idea how to make this work? Thanks, Jeff
Advertisement
What do you mean by open programs? Are you looking for windows of your programs that aren't minimized? Are you just looking for any windows of your programs? Or do you just want to get handles to ALL processes on your computer?
Not all processes, just windows of open programs, whether they're minimized or not.
An application's main window is defined as a window which has no owner. The only test you should need is

if (GetWindow(hwnd, GW_OWNER) == NULL) {   // this is an application's main window}


Note, returning FALSE from EnumWindowProc will cause the enumeration to halt. You dont want that in this case. Always return TRUE otherwise you'll miss some (many) windows.
I tried the following:
BOOL _stdcall Dummy(HWND WindowHandle, LPARAM){	if(::GetParent(WindowHandle) || !::IsWindowVisible(WindowHandle) || !(::GetWindowLong(WindowHandle, GWL_STYLE) & WS_OVERLAPPEDWINDOW))		return TRUE;	char szWindowText[1024] = {};	::GetWindowText(WindowHandle, szWindowText, sizeof(szWindowText));	std::cout << szWindowText << std::endl;	return TRUE;}

It picked up all the windows in the taskbar.
Keep in mind that this will not filter Explorer windows.
If those bug you, then you can use GetWindowThreadProcessId on the window handle and compare the PID to the PID of "Explorer.exe".
Thanks everybody! So far so good!
This is a purely subjective style nitpick, but when you're checking a non-Boolean function for a NULL return, it's more clear to write if(Foo() != NULL) or if(Foo() != 0) or even if(Foo()).

Your original formulation (if(!Foo() == 0)) is redundant, uncommon (and therefore likely to cause double-takes and/or confusion), and, if it turns into a style habit, could really screw you royally if you ever get into situations involving operator overloading that presents different boolean semantics than you expect.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

This topic is closed to new replies.

Advertisement