pip install pywin32  -i https://pypi.tuna.tsinghua.edu.cn/simple  


import win32gui
# 获取窗口句柄
hwnd = win32gui.FindWindow(None, '命令提示符')
# 获取窗口文本内容
text = win32gui.GetWindowText(hwnd)
print(text)
# 获取窗口的类名
className = win32gui.GetClassName(hwnd)
print(className)
# 获取窗口的位置和大小
left, top, right, bottom = win32gui.GetWindowRect(hwnd)
width = right - left
height = bottom - top
print('left: %s, top: %s, width: %s, height: %s' % (left, top, width, height))

========================

import win32gui


# 查找窗口句柄
def getHwnd(clzName,title):
    hwnd = win32gui.FindWindow(clzName, title)
    return hwnd

# 获取窗口标题
def getTitle(hwnd):
    return win32gui.GetWindowText(hwnd)

# 查找控件句柄
def getChildHwnd(hwnd,parentHwnd,clzName,winName):
    return win32gui.FindWindowEx(hwnd, parentHwnd, clzName, winName)

# 窗口位置大小
def get_window_pos_size(hwnd):
    window_rect = win32gui.GetWindowRect(hwnd)

    x = window_rect[0]
    y = window_rect[1]
    width = window_rect[2] - x
    height = window_rect[3] - y

    return x, y, width, height
 

控制窗口状态


最小化窗口:win32gui.MinimizeWindow(handle)
最大化窗口:win32gui.MaximizeWindow(handle)
还原窗口:win32gui.RestoreWindow(handle)
激活窗口:win32gui.SetActiveWindow(handle)
置顶窗口:win32gui.SetForegroundWindow(handle)
取消置顶:win32gui.SetForegroundWindow(0)

控制窗口大小和位置


设置窗口大小:win32gui.SetWindowPos(handle, None, x, y, width, height, flags)
获取窗口大小:win32gui.GetWindowRect(handle)
移动窗口:win32gui.MoveWindow(handle, x, y, width, height, repaint)

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐