相关疑难解决方法(0)

获取每个窗口的HWND?

我正在开发一个python应用程序,我想获得HWND每个打开的窗口.我需要窗口的名称和HWND过滤列表来管理一些特定的窗口,移动和调整它们的大小.

我试图自己查看信息,但我没有得到正确的代码.我试过这个代码,但我只得到每个窗口的标题(这很棒),但我也需要它HWND.

import ctypes
import win32gui
EnumWindows = ctypes.windll.user32.EnumWindows
EnumWindowsProc = ctypes.WINFUNCTYPE(ctypes.c_bool, ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int))
GetWindowText = ctypes.windll.user32.GetWindowTextW
GetWindowTextLength = ctypes.windll.user32.GetWindowTextLengthW
IsWindowVisible = ctypes.windll.user32.IsWindowVisible

titles = []
def foreach_window(hwnd, lParam):
    if IsWindowVisible(hwnd):
        length = GetWindowTextLength(hwnd)
        buff = ctypes.create_unicode_buffer(length + 1)
        GetWindowText(hwnd, buff, length + 1)
        titles.append((hwnd, buff.value))
    return True
EnumWindows(EnumWindowsProc(foreach_window), 0)

for i in range(len(titles)):
    print(titles)[i]

win32gui.MoveWindow((titles)[5][0], 0, 0, 760, 500, True)
Run Code Online (Sandbox Code Playgroud)

这里有一个错误:

win32gui.MoveWindow((titles)[5][0], 0, 0, 760, 500, True)
 TypeError: The object is …
Run Code Online (Sandbox Code Playgroud)

python windows hwnd

7
推荐指数
1
解决办法
3万
查看次数

标签 统计

hwnd ×1

python ×1

windows ×1