admin
2023-03-29 7aaca048ffc7666b0eccf3d8b7ba212bae3c9e13
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import array
 
import cv2
import numpy
import win32con
import win32gui
import win32ui
 
 
def getText(hwnd):
    bufSize = win32gui.SendMessage(hwnd, win32con.WM_GETTEXTLENGTH, 0, 0) + 1
    buffer = array.array('b', b'\x00\x00' * bufSize)
    win32gui.SendMessage(hwnd, win32con.WM_GETTEXT, bufSize, buffer)
    text = win32gui.PyGetString(buffer.buffer_info()[0], bufSize - 1)
    return text.replace("\x00", "").strip()
 
 
# 根据标题模糊匹配
def search_window(title):
    hwnds = []
    hwnd = win32gui.GetDesktopWindow()
    temp = None
    while True:
        if temp and win32gui.IsWindowVisible(temp):
            str_ = getText(temp)
            if str_.find(title) > -1:
                hwnds.append(temp)
                print(str_)
        temp = win32gui.FindWindowEx(hwnd, temp, None, None)
        if not temp:
            break
    return hwnds
 
 
def visual_click(hwnd, pos):
    position = pos[1] * pow(16, 4) + pos[0]
    win32gui.PostMessage(hwnd, win32con.WM_LBUTTONDOWN, 0x00000001, position)
    win32gui.PostMessage(hwnd, win32con.WM_LBUTTONUP, 0x00000000, position)
 
 
def visual_keyboard(hwnd, code):
    win32gui.PostMessage(hwnd, win32con.WM_KEYDOWN, code, 0)
    win32gui.PostMessage(hwnd, win32con.WM_KEYUP, code, 0)
 
 
def window_capture(hwnd, rect):
    w = rect[2] - rect[0]
    h = rect[3] - rect[1]
    if w == 0 or h == 0:
        return None
    # 返回句柄窗口的设备环境,覆盖整个窗口,包括非客户区,标题栏,菜单,边框
    hWndDC = win32gui.GetWindowDC(hwnd)
    # 创建设备描述表
    mfcDC = win32ui.CreateDCFromHandle(hWndDC)
    # 创建内存设备描述表
    saveDC = mfcDC.CreateCompatibleDC()
    # 创建位图对象准备保存图片
    saveBitMap = win32ui.CreateBitmap()
    # 为bitmap开辟存储空间
    saveBitMap.CreateCompatibleBitmap(mfcDC, w, h)
    # 将截图保存到saveBitMap中
    saveDC.SelectObject(saveBitMap)
    # 保存bitmap到内存设备描述表
    saveDC.BitBlt((0, 0), (w, h), mfcDC, (rect[0], rect[1]), win32con.SRCCOPY)
    # 保存数据
    saveBitMap.SaveBitmapFile(saveDC, "test.png")
 
    signedIntsArray = saveBitMap.GetBitmapBits(True)
 
 
 
    # 内存释放
    win32gui.DeleteObject(saveBitMap.GetHandle())
    saveDC.DeleteDC()
    mfcDC.DeleteDC()
    win32gui.ReleaseDC(hwnd, hWndDC)
 
    im_opencv = numpy.frombuffer(signedIntsArray, dtype='uint8')
    im_opencv.shape = (h, w, 4)
    cv2.cvtColor(im_opencv, cv2.COLOR_BGRA2RGB)
    return im_opencv
 
 
if __name__ == "__main__":
    print(search_window("副屏1"))
    visual_click(0x00152876, (110, 90))