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

时间:2022-09-20 20:57:11

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 devoloper,但如果有一个跨平台的方法,它不会让我感到不安。

3 个解决方案

#1


12  

Using hints from WindowMover article and Nattee Niparnan's blog post I 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 module for this to work.

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

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

编辑:我发现GetWindowRect提供的结果比GetClientRect更正确。来源已更新。

#2


8  

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

#3


2  

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

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

#1


12  

Using hints from WindowMover article and Nattee Niparnan's blog post I 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 module for this to work.

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

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

编辑:我发现GetWindowRect提供的结果比GetClientRect更正确。来源已更新。

#2


8  

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

#3


2  

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

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