在 Python 中获取其他正在运行的进程窗口大小 [英] Get other running processes window sizes in Python

查看:58
本文介绍了在 Python 中获取其他正在运行的进程窗口大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

使用来自 WindowMover 的提示文章Nattee Niparnan 的博文 我设法创建了这个:>

Using hints from WindowMover article and Nattee Niparnan's blog post I managed to create this:

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

您需要 Python 模块的 Win32 扩展才能使其工作.

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

我发现 GetWindowRectGetClientRect 给出更正确的结果.来源已更新.

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

这篇关于在 Python 中获取其他正在运行的进程窗口大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆