python windows操作常用方法

2021-12-09  本文已影响0人  mutang
class HandleWin:

    def __init__(self, class_name='Chrome_WidgetWin_1'):
        self.class_name = class_name
        self.open_app()
        self.hwnd = self.find_window_by_title()
        self.window_showmax()

    def get_current_window(self):
        '''获取当前窗口'''
        return win32gui.GetForegroundWindow()

    def set_current_window(self, hwnd):
        '''
        窗口置前
        '''
        win32gui.SetForegroundWindow(hwnd)

    def get_window_title(self, hwnd):
        '''根据句柄得到标题'''
        return win32gui.GetWindowText(hwnd)

    def get_current_window_title(self):
        '''得到当前句柄的标题'''
        return self.get_window_title(self.get_current_window())

    def find_window_by_title(self):
        '''查找'''
        try:
            return win32gui.FindWindow(self.class_name, None)  # win32gui.FindWindow(None, title)
        except Exception as ex:
            print('error calling win32gui.FindWindow ' + str(ex))
            return

    def get_child_windows(self, parent):
        '''
        获得parent的所有子窗口句柄
         返回子窗口句柄列表
         '''
        if not parent:
            return
        hwndChildList = []
        win32gui.EnumChildWindows(parent, lambda hwnd, param: param.append(hwnd), hwndChildList)
        return hwndChildList

    def open_app(self):
        win32api.ShellExecute(0, 'open', BROWSER_PATH, '', '', 3)
        time.sleep(2)

    def window_showmax(self):
        win32gui.ShowWindow(self.hwnd, win32con.SW_SHOWMAXIMIZED)


import win32gui
import win32api
import pyautogui
hwnd_title = {}

def get_all_hwnd(hwnd, mouse):
    if (win32gui.IsWindow(hwnd) and
        win32gui.IsWindowEnabled(hwnd) and
        win32gui.IsWindowVisible(hwnd)):
        hwnd_title.update({hwnd: win32gui.GetWindowText(hwnd)})


win32gui.EnumWindows(get_all_hwnd, 0)

# m = PyMouse()

for h, t in hwnd_title.items():
    if t :
        print(h, t)
        if t == '米格尔':
            left, top, right, bottom = win32gui.GetWindowRect(h)
            print(left,top,right,bottom)
            pyautogui.click(right-206,bottom-31)


上一篇 下一篇

猜你喜欢

热点阅读