zoukankan      html  css  js  c++  java
  • 利用python+win32api获取标题对应的窗口句柄id,并且操作为当前活动窗口

    # #!/usr/bin/python3
    # -*- coding: utf-8 -*-
    # @Time : 2021-06-15 18:08
    # @Author : BruceLong
    # @FileName: switch_win.py
    # @Email   : 18656170559@163.com
    # @Software: PyCharm
    # @Blog :http://www.cnblogs.com/yunlongaimeng/
    import ctypes
    import win32gui
    
    import win32con
    
    
    def get_jb_id(title):
        '''
        根据标题找句柄
        :param title: 标题
        :return:返回句柄所对应的ID
        '''
        jh = []
        hwnd_title = dict()
    
        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)
        for h, t in hwnd_title.items():
            if t is not "":
                if title in t:
                    jh.append(h)
    
        if len(jh) == 0:
            print("找不到相应的句柄")
        else:
            return jh
    
    
    def switch_roles(hwnd):
        '''
        根据句柄id切换活动窗口
        :param hwnd: 
        :return: 
        '''
        try:
            ctypes.windll.user32.SwitchToThisWindow(hwnd, True)
            win32gui.ShowWindow(hwnd, win32con.SW_SHOWNORMAL)
            win32gui.SetForegroundWindow(hwnd)
            msg = [True, 'exec success', None]
        except Exception as e:
            msg = [False, '没有找到可操作的对象', str(e)]
        return msg
    
    
    jb_id_list = get_jb_id("Studio 3T for MongoDB - TRIAL LICENSE")
    jb_id = jb_id_list[0] if jb_id_list else jb_id_list
    print(switch_roles(jb_id))
  • 相关阅读:
    使用setTimeout()代替setInterval()
    音视频入门-02-RGB拼图
    音视频入门-01-认识RGB
    CMake入门-04-自定义编译选项
    CMake入门-03-还是HelloWorld
    CMake入门-02-HelloWorld扩展
    CMake入门-01-从HelloWorld开始
    HTML页面启动sass监听编译成css文件
    安装+配置Express
    安装+配置Nginx
  • 原文地址:https://www.cnblogs.com/yunlongaimeng/p/14886732.html
Copyright © 2011-2022 走看看