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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/151846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 11:20:26  来源:igfitidea点击:

Get other running processes window sizes in Python

pythonwindowswinapipywin32

提问by Brian Paden

This isn't as malicious as it sounds, I want to get the current size of their windows, not look at what is in them. The purpose is to figure out that if every other window is fullscreen then I should start up like that too. Or if all the other processes are only 800x600 despite there being a huge resolution then that is probably what the user wants. Why make them waste time and energy resizing my window to match all the others they have? I am primarily a Windows devoloper but it wouldn't upset me in the least if there was a cross platform way to do this.

这并不像听起来那么恶意,我想获取他们窗口的当前大小,而不是查看其中的内容。目的是弄清楚如果所有其他窗口都是全屏的,那么我也应该这样启动。或者,如果尽管分辨率很高,但所有其他进程只有 800x600,那么这可能是用户想要的。为什么要让他们浪费时间和精力调整我的窗口大小以匹配他们拥有的所有其他窗口?我主要是一个 Windows 开发者,但如果有一种跨平台的方式来做到这一点,它至少不会让我感到不安。

回答by DzinX

Using hints from WindowMover articleand Nattee Niparnan's blog postI managed to create this:

使用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

You need the Win32 Extensions for Python modulefor this to work.

您需要Win32 Extensions for Python 模块才能使其工作。

EDIT: I discovered that GetWindowRectgives more correct results than GetClientRect. Source has been updated.

编辑:我发现GetWindowRectGetClientRect. 来源已更新。

回答by Dustin Wyatt

I'm a big fan of AutoIt. They have a COM version which allows you to use most of their functions from Python.

我是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

回答by Swaroop C H

Check out the win32guimodulein the Windows extensions for Python. It may provide some of the functionality you're looking for.

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

回答by user3881450

I updated the GREAT @DZinX code adding the title/text of the windows:

我更新了 GREAT @DZinX 代码,添加了窗口的标题/文本:

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)
    text = win32gui.GetWindowText(hWnd)
    windows.append((hWnd, (rect[2] - rect[0], rect[3] - rect[1]), text ))
windows = []
win32gui.EnumWindows(callback, windows)
return windows

for win in getWindowSizes():
print(win)