Jump to content
  • Advertisement
Sign in to follow this  
GodlyGamr

Enumerating Desktop Windows

This topic is 4485 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
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?

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!