雷电模拟器句柄相关01

2022-06-09

获取雷电模拟器的句柄

 

# # !/usr/bin/env python
# # -*-coding:utf-8 -*-
#
# """
# # File       : yunbiao.py
# # Time       :2022/6/09
# # Author     :Sail
# # email      : 274363903@qq.com
# # Description:
#
# """

import win32gui
import win32con
import sys
from PyQt5.QtWidgets import QApplication


class Hwnd():
    def get_hwnd_dic(self, hwnd, hwnd_title):
        if (win32gui.IsWindow(hwnd)
                and win32gui.IsWindowEnabled(hwnd)
                and win32gui.IsWindowVisible(hwnd)
                and win32gui.GetWindowText(hwnd)):
            hwnd_title[f"{hwnd}"] = win32gui.GetWindowText(hwnd)

    def get_hwnd(self):
        """
        :return: {hwnd:title}
        """
        hwnd_title = {}
        win32gui.EnumWindows(self.get_hwnd_dic, hwnd_title)
        return hwnd_title


hwnd = Hwnd()
print(hwnd.get_hwnd())

# 返回窗口标题为"雷电模拟器"句柄
hld_leidian = win32gui.FindWindow(None, u"雷电模拟器")
print("雷电模拟器窗口句柄ID:" + str(hld_leidian))


# 根据句柄获取句柄标题和类名
def get_child_windows(parent):
    """
    获得parent的所有子窗口句柄
    返回子窗口句柄列表
    :param parent: parent为父窗口句柄id
    :return:
    """

    if not parent:
        return
    hwndchildlist = []
    win32gui.EnumChildWindows(parent, lambda hwnd, param: param.append(hwnd),  hwndchildlist)
    return hwndchildlist


ret_leidian = get_child_windows(hld_leidian)
print(ret_leidian)


# 根据句柄获取句柄标题和类名
if ret_leidian is not None:
    for i in ret_leidian:
        title = win32gui.GetWindowText(i)  # i为句柄id
        # 获取标题
        clsname = win32gui.GetClassName(i)
        # 获取类名
        print(title, clsname)
        # 获取左、上、右、下的窗口位置
        left, top, right, bottom = win32gui.GetWindowRect(i)
        print(left, top, right, bottom)
        print("id:{id}、title:{title}、clsname:{clsname}、left:{left}, top:{top}, right:{right}, bottom:{bottom}"
              .format(id=i, title=title, clsname=clsname, left=left, top=top, right=right, bottom=bottom))


# 使用PyQt5 进行雷电截图
# 雷电窗口可以遮挡,但不可最小化,最小化截图黑屏
app = QApplication(sys.argv)
screen = QApplication.primaryScreen()
img = screen.grabWindow(ret_leidian[0]).toImage()

img.save("screenshot.jpg")  # 大小150k
img.save("screenshot.png")  # 大小约600k

# 关闭雷电模拟器窗口
#win32gui.PostMessage(hld_leidian, win32con.WM_CLOSE, 0, 0)  # 参数1 窗口句柄

 

C:\Users\Administrator\PycharmProjects\pythonProject2\venv\Scripts\python.exe C:/Users/Administrator/PycharmProjects/test/test.py
{'788210': 'test – test.py Administrator', '263830': '博客后台 - 博客园 - Google Chrome', '984074': 'screenshot.jpg - 画图', '393810': 'Microsoft Text Input Application', '263542': 'C:\\Users\\Administrator\\PycharmProjects\\test', '657124': '雷电模拟器', '131440': 'Program Manager'}
雷电模拟器窗口句柄ID:657124
[67368, 132978]
TheRender RenderWindow
-31999 -31966 -30719 -31246
id:67368、title:TheRender、clsname:RenderWindow、left:-31999, top:-31966, right:-30719, bottom:-31246
sub subWin
-31999 -31966 -30719 -31246
id:132978、title:sub、clsname:subWin、left:-31999, top:-31966, right:-30719, bottom:-31246

Process finished with exit code 0

雷电模拟器的Rrestored Rect(还原矩形)1282*756,其内嵌的游戏窗口为1280*720

雷电和游戏的像素差:(x1+1 ,y1+4) -(x2-1, y2--2)

 

     

 

 

 

  

 

 

      

 

posted on 2022-06-09 22:28  sailxu  阅读(1306)  评论(0)    收藏  举报

导航