簡體   English   中英

使用python獲取窗口位置和大小

[英]Get window position & size with python

如何使用 python 獲取和設置窗口(任何 Windows 程序)的位置和大小?

假設您使用的是 Windows,請嘗試使用pywin32win32gui模塊及其EnumWindowsGetWindowRect函數。

如果您使用的是 Mac OS X,則可以嘗試使用appscript

對於 Linux,您可以嘗試 X11 的眾多接口之一。

編輯: Windows 示例(未測試):

import win32gui

def callback(hwnd, extra):
    rect = win32gui.GetWindowRect(hwnd)
    x = rect[0]
    y = rect[1]
    w = rect[2] - x
    h = rect[3] - y
    print("Window %s:" % win32gui.GetWindowText(hwnd))
    print("\tLocation: (%d, %d)" % (x, y))
    print("\t    Size: (%d, %d)" % (w, h))

def main():
    win32gui.EnumWindows(callback, None)

if __name__ == '__main__':
    main()

您可以使用GetWindowRect函數獲取窗口坐標。 為此,您需要一個窗口句柄,您可以使用FindWindow獲取該句柄,假設您了解有關窗口的一些信息(例如其標題)。

要從 Python 調用 Win32 API 函數,請使用pywin32

對於 Linux,您可以使用我在這里制作的工具。 該工具的用途略有不同,但您可以根據需要直接使用 API。

安裝工具

sudo apt-get install xdotool xprop xwininfo
git clone https://github.com/Pithikos/winlaunch.git && cd winlaunch

在終端

>>> from winlaunch import *
>>> wid, pid = launch('firefox')
>>> win_pos(wid)
[3210, 726]

widpid代表窗口 ID 和進程 ID。

此代碼將在 Windows 上工作。 它返回活動窗口的位置和大小。

from win32gui import GetWindowText, GetForegroundWindow
print(GetWindowRect(GetForegroundWindow()))

這可以從窗口標題返回窗口矩形

代碼

def GetWindowRectFromName(name:str)-> tuple:
    hwnd = ctypes.windll.user32.FindWindowW(0, name)
    rect = ctypes.wintypes.RECT()
    ctypes.windll.user32.GetWindowRect(hwnd, ctypes.pointer(rect))
    # print(hwnd)
    # print(rect)
    return (rect.left, rect.top, rect.right, rect.bottom)

if __name__ == "__main__":
    print(GetWindowRectFromName('CALC'))
    pass

環境

Python 3.8.2 | 由 conda-forge 打包 | (默認,2020 年 4 月 24 日,07:34:03)在 win32 Windows 10 Pro 1909 上的 [MSC v.1916 64 位 (AMD64)]

正如 Greg Hewgill 所提到的,如果您知道窗口的名稱,您可以簡單地使用win32gui的 FindWindow 和 GetWindowRect。 這可能比以前的方法更清潔和有效。

from win32gui import FindWindow, GetWindowRect

# FindWindow takes the Window Class name (can be None if unknown), and the window's display text. 
window_handle = FindWindow(None, "Diablo II")
window_rect   = GetWindowRect(window_handle)

print(window_rect)
#(0, 0, 800, 600)

供將來參考:PyWin32GUI 現已移至 Github

其他任何回復中都沒有提到的是,在較新的 Windows(Vista 及更高版本)中,“ Window Rect 現在包括陰影占據的區域。 ”,這就是win32gui.GetWindowRectctypes.windll.user32.GetWindowRect正在與。

如果你想在沒有陰影的情況下獲得位置和大小,你可以:

  1. 手動刪除它們。 就我而言,左側、底部和右側有 10 個像素必須修剪。
  2. 使用dwmapi提取文章中提到的DWMWA_EXTENDED_FRAME_BOUNDS

關於使用dwmapi.DwmGetWindowAttribute (見這里):

這個函數有四個參數:hwnd,我們感興趣的屬性的標識符,寫入屬性的數據結構的指針,這個數據結構的大小。 我們通過檢查這個 enum得到的標識符。 在我們的例子中,屬性DWMWA_EXTENDED_FRAME_BOUNDS位於位置 9。

import ctypes
from ctypes.wintypes import HWND, DWORD, RECT

dwmapi = ctypes.WinDLL("dwmapi")

hwnd = 133116    # refer to the other answers on how to find the hwnd of your window

rect = RECT()
DMWA_EXTENDED_FRAME_BOUNDS = 9
dwmapi.DwmGetWindowAttribute(HWND(hwnd), DWORD(DMWA_EXTENDED_FRAME_BOUNDS),
                             ctypes.byref(rect), ctypes.sizeof(rect))

print(rect.left, rect.top, rect.right, rect.bottom)

最后:“請注意,與 Window Rect 不同,DWM Extended Frame Bounds 未針對 DPI 進行調整”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM