Sign in to follow this  
GodlyGamr

Enumerating Desktop Windows

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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".

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this