-
Notifications
You must be signed in to change notification settings - Fork 0
/
pap_win32.py
183 lines (157 loc) · 5.77 KB
/
pap_win32.py
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# -*- coding:utf-8 -*-
import base64
import ctypes
import datetime
import os
import re
import sys
import time
from io import BytesIO, TextIOWrapper
import pyscreeze
import win32api
import win32con
import win32gui
import win32ui
from PIL import Image
from .utils import PAGException
from typing import Union, Optional, Any
__key={
'mouse_left':[win32con.WM_LBUTTONDOWN,win32con.WM_LBUTTONUP,win32con.MK_LBUTTON],
'mouse_middle':[win32con.WM_MBUTTONDOWN,win32con.WM_MBUTTONUP,win32con.MK_MBUTTON],
'mouse_right':[win32con.WM_RBUTTONDOWN,win32con.WM_RBUTTONUP,win32con.MK_RBUTTON],
'kb_shift':[win32con.MK_SHIFT],
}
class _POINT(ctypes.Structure):
_fields_ = [("x", ctypes.c_long),
("y", ctypes.c_long)]
class PyAutoPlay_win32:
"""base class"""
def __init__(self):
self.img_type = 'JPG'
self.left = 0
self.right = 0
self.bottom = 0
self.top = 0
self.width = 0
self.height = 0
self.x = 0
self.y = 0
self.hwnd = None
self.title = ''
def set_hwnd(self, hwnd: Optional[int]) -> None:
self.hwnd = hwnd
def sleep(self, _time: Optional[int]) -> None:
return time.sleep(_time)
def position(self) -> tuple:
cursor = _POINT()
ctypes.windll.user32.GetCursorPos(ctypes.byref(cursor))
return (cursor.x, cursor.y)
def get_all_id_title(self) -> dict:
'''return a dict contains all hwnd and title'''
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)
return hwnd_title
def find_title_custom(self, window_name: Optional[str]) -> dict:
''' find_title_custom'''
self.title = window_name
hwnd_title = self.get_all_hwnd_title()
match_hwnd_title = dict()
for h, t in hwnd_title.items():
if window_name in t:
match_hwnd_title.update({h: t})
return match_hwnd_title
def find_title(self, window_name: Optional[str]) -> int:
''' return the first match hwnd(int) '''
hwnd_title = self.find_title_custom(window_name)
for h, t in hwnd_title.items():
if window_name in t:
return int(h)
raise PAGException("ERROR: title '{0}' not found!".format(window_name))
def find_all_title(self, window_name: Optional[str]) -> dict:
"""return a dict contains all matched hwnd and title"""
if m_h_t := self.find_title_custom(window_name) != {}:
return m_h_t
raise PAGException("ERROR: title '{0}' not found!".format(window_name))
def get_screenshot(self) -> Image.Image:
"""get screenshot
"""
hwnd = int(self.hwnd)
left, top, right, bottom = win32gui.GetWindowRect(hwnd)
width = right - left
height = bottom - top
self.left = left
self.top = top
self.right = right
self.bottom = bottom
self.height = height
self.width = width
hWndDC = win32gui.GetWindowDC(hwnd)
mfcDC = win32ui.CreateDCFromHandle(hWndDC)
saveDC = mfcDC.CreateCompatibleDC()
saveBitMap = win32ui.CreateBitmap()
saveBitMap.CreateCompatibleBitmap(mfcDC, width, height)
saveDC.SelectObject(saveBitMap)
saveDC.BitBlt((0, 0), (width, height), mfcDC, (0, 0), win32con.SRCCOPY)
bmpinfo = saveBitMap.GetInfo()
bmpstr = saveBitMap.GetBitmapBits(True)
im_PIL = Image.frombuffer(
"RGB",
(bmpinfo["bmWidth"], bmpinfo["bmHeight"]),
bmpstr,
"raw",
"BGRX",
0,
1,
)
return im_PIL
def locate(self, image, type='', **kwargs):
if type == "base64":
needle_image = BytesIO(base64.b64decode(image))
else:
needle_image = Image.open(image)
haystack_image = self.get_screenshot()
if res := pyscreeze.locate(needle_image, haystack_image) != None:
position = []
self.x = pyscreeze.center(res)[0]
self.y = pyscreeze.center(res)[1]
position.append(pyscreeze.center(res)[0])
position.append(pyscreeze.center(res)[1])
return True
return False
def click_custom(self, pos: Optional[list], key: Optional[str] = 'mouse_left', hold_time=0.05):
''' click custom'''
_pos = win32api.MAKELONG(pos[0], pos[1])
if key in ['mouse_left','mouse_middle','mouse_right']:
_key=__key['key']
else:
raise PAGException("key {0} not found, please check available list".format(key))
win32gui.SendMessage(self.hwnd, _key[0], win32con.WA_ACTIVE, 0)
win32api.SendMessage(self.hwnd, win32con.WM_LBUTTONDOWN,win32con.MK_LBUTTON, _pos)
self.sleep(hold_time)
win32api.SendMessage(self.hwnd, win32con.WM_LBUTTONUP, win32con.MK_LBUTTON, _pos)
def sendkey(self, image, key: str, times=1, interval=0.1):
"""
Send specified key to the matched image.
Parameter:
- image: image to be matched
- key: send mouse click or press from keyboard
- times: click times
"""
pass
def sendkeys(self, *_image_list: list, key: str, times=1, interval=0.1):
"""
Send same key to all matched images.
Parameter:
- _image_list: list of images
- key: send mouse click or press sth
- times: each matched image click frequency
- interval: send key interval
"""
pass