I am running following script in order to find out whether some process has any visible windows:
import win32gui
import win32process
pid = 6324
def WindowIsVisible(pid):
data = [pid, False]
win32gui.EnumWindows(enumHandler, data)
return data[1]
def enumHandler(hwnd, data):
if win32process.GetWindowThreadProcessId(hwnd)[1] == data[0] and win32gui.IsWindowVisible(hwnd):
data[1] = True
if WindowIsVisible(pid):
print "has visible window"
else:
print "does not have visible window"
For some reason, it prints has visible window
However, this is what my taskbar and task manager looks like:
How is that possible? Outlook exe is definitely invisible (although it's visible in notification icon area)
That IsWindowVisible
returns True
does not mean that you will be able to see a window on the screen. The window could be minimized, for instance. So, you probably need to check that the window is both visible and not minimized at the very leasy. Use win32gui.IsIconic()
to test for the window being minimized.
It is also entirely possible that Outlook has multiple top-level windows, as was covered at your previous question. Your approach will detect whether or not any of those windows has the visible property.
Your code also looks quite odd. Passing a PID to a function named WindowIsVisible
feels wrong to me. That's a question you would ask of a window rather than a process.
I think that this code will be more appropriate to your needs:
def topLevelWindows(pid):
def enumHandler(hwnd, data):
if win32process.GetWindowThreadProcessId(hwnd)[1] == pid:
windows.append(hwnd)
return True
windows = []
win32gui.EnumWindows(enumHandler, 0)
return windows
for hwnd in topLevelWindows(pid):
if win32gui.IsWindowVisible(hwnd) and not win32gui.IsIconic(hwnd):
print '%.8x %s' % (hwnd, win32gui.GetWindowText(hwnd))
However, this will still enumerate all top-level windows. And I think what you are really looking for is the Outlook main window. I suspect that you'll need to find some way to identify that window.