在Python中获取其他正在运行的进程窗口大小

这并不像听起来那么恶毒,我想获得当前窗口的大小,而不是看它们的内容。 目的是要弄清楚,如果其他窗口全屏显示,那么我也应该这样启动。 或者如果所有其他进程只有800×600,尽pipe有一个巨大的决议,那么这可能是用户想要的。 为什么让他们浪费时间和精力重新调整我的窗口,以匹配所有其他人呢? 我主要是一个Windows devoloper,但是如果有跨平台的方式来做到这一点的话,它不会让我感到不安。

从WindowMover文章和Nattee Niparnan的博客 文章中使用提示,我设法创建了这个:

import win32con import win32gui def isRealWindow(hWnd): '''Return True iff given window is a real Windows application window.''' if not win32gui.IsWindowVisible(hWnd): return False if win32gui.GetParent(hWnd) != 0: return False hasNoOwner = win32gui.GetWindow(hWnd, win32con.GW_OWNER) == 0 lExStyle = win32gui.GetWindowLong(hWnd, win32con.GWL_EXSTYLE) if (((lExStyle & win32con.WS_EX_TOOLWINDOW) == 0 and hasNoOwner) or ((lExStyle & win32con.WS_EX_APPWINDOW != 0) and not hasNoOwner)): if win32gui.GetWindowText(hWnd): return True return False def getWindowSizes(): ''' Return a list of tuples (handler, (width, height)) for each real window. ''' def callback(hWnd, windows): if not isRealWindow(hWnd): return rect = win32gui.GetWindowRect(hWnd) windows.append((hWnd, (rect[2] - rect[0], rect[3] - rect[1]))) windows = [] win32gui.EnumWindows(callback, windows) return windows for win in getWindowSizes(): print win 

您需要Win32的Python扩展模块才能正常工作。

编辑:我发现GetWindowRectGetClientRect更正确的结果。 来源已更新。

我是AutoIt的忠实粉丝。 他们有一个COM版本,它允许你使用Python的大部分功能。

 import win32com.client oAutoItX = win32com.client.Dispatch( "AutoItX3.Control" ) oAutoItX.Opt("WinTitleMatchMode", 2) #Match text anywhere in a window title width = oAutoItX.WinGetClientSizeWidth("Firefox") height = oAutoItX.WinGetClientSizeHeight("Firefox") print width, height 

查看Python的Windows扩展中的win32gui模块 。 它可能会提供您正在寻找的一些功能。