【问题标题】:Find a application width and height - Crossplatform查找应用程序的宽度和高度 - 跨平台
【发布时间】:2012-01-25 00:03:34
【问题描述】:

我正在寻找一种方法来查找指定的窗口尺寸(宽度和高度)。到目前为止,我一直在使用 AutoIt 功能,只需编写部分窗口/框架名称,它的工作方式就像我想要的那样。唯一的问题是它只适用于 MS Windows。我需要它跨平台工作(windows & linux)。

由于平台如此不同,因此需要有两个脚本,一个如果系统是 windows,一个用于 Linux。而且我不想依赖额外的程序(比如 AutoIt)。我不希望它在脚本中“硬编码”选择什么框架。我需要/希望它像 AutoIt 一样工作,通过指定框架名称/或其部分。

【问题讨论】:

  • @SLACKY:程序或框架尺寸是什么意思?是您感兴趣的窗口吗(正如 yurib 建议的那样)?或者,是你想弄清楚的屏幕分辨率吗?

标签: python height width frame


【解决方案1】:

感谢 yurib,我想出了如何在 Windows 中做到这一点。

import win32con
import win32gui

def inText(haystack, needle, n):
    parts= haystack.split(needle, n+1)
    if len(parts)<=n+1:
        return False
    if len(haystack)-len(parts[-1])-len(needle):
        return True

def isRealWindow(hWnd):
    '''Return True if 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(text):
    '''Return a list of tuples (handler, (width, height)) for each real window.'''
    def callback(hWnd, extra):
        if not isRealWindow(hWnd):
            return
        title   = win32gui.GetWindowText(hWnd)
        rect    = win32gui.GetWindowRect(hWnd)
        isFrame = inText(title, text, 0)
        if(isFrame):
            windows.append((title, rect[2] - rect[0], rect[3] - rect[1], rect[0],rect[1]))
    windows = []
    win32gui.EnumWindows(callback, windows)
    return windows

def findWindow(text):
    window = getWindowSizes(text)
    name = window[0][0]
    w = window[0][1]
    h = window[0][2]
    x = window[0][3]
    y = window[0][4]

    return name,w,h,x,y

现在我需要知道如何在 Linux 中做这样的事情。有什么提示吗?

【讨论】:

    猜你喜欢
    • 2018-01-14
    • 2021-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-14
    • 1970-01-01
    相关资源
    最近更新 更多