-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWin32GUI.py
More file actions
293 lines (268 loc) · 11.5 KB
/
Win32GUI.py
File metadata and controls
293 lines (268 loc) · 11.5 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import time
import ctypes
from ctypes import wintypes
import win32api
import win32con
import win32gui
'''
ref:
https://gist.github.com/mouseroot/6128651
http://www.christophekeller.com/hello-world-in-python-using-win32/
https://stackoverflow.com/questions/5353883/python3-ctype-createwindowex-simple-example
http://code.activestate.com/recipes/208699-calling-windows-api-using-ctypes-and-win32con/
https://riptutorial.com/winapi/example/13881/create-a-new-thread
https://stackoverflow.com/questions/50576770/python-ctype-windll-kernel32-createthread-parameter-value
https://blog.csdn.net/tangaowen/article/details/6054123
'''
GetMessageA = ctypes.windll.user32.GetMessageA
GetMessageW = ctypes.windll.user32.GetMessageW
GetModuleHandleA = ctypes.windll.kernel32.GetModuleHandleA
GetModuleHandleW = ctypes.windll.kernel32.GetModuleHandleW
CreateWindowExA = ctypes.windll.user32.CreateWindowExA
CreateWindowExW = ctypes.windll.user32.CreateWindowExW
RegisterClassExA = ctypes.windll.user32.RegisterClassExA
RegisterClassExW = ctypes.windll.user32.RegisterClassExW
DispatchMessageA = ctypes.windll.user32.DispatchMessageA
DispatchMessageW = ctypes.windll.user32.DispatchMessageW
CreateThread = ctypes.windll.kernel32.CreateThread
SetLayeredWindowAttributes = ctypes.windll.user32.SetLayeredWindowAttributes
LPTHREAD_START_ROUTINE = ctypes.WINFUNCTYPE(ctypes.wintypes.DWORD, ctypes.wintypes.LPVOID)
WNDPROCTYPE = ctypes.WINFUNCTYPE(ctypes.c_int, wintypes.HWND, ctypes.c_uint, wintypes.WPARAM, wintypes.LPARAM)
class WNDCLASSEX(ctypes.Structure): # tagWNDCLASSEXA
_fields_ = [("cbSize" , ctypes.c_uint),
("style" , ctypes.c_uint),
("lpfnWndProc" , WNDPROCTYPE),
("cbClsExtra" , ctypes.c_int),
("cbWndExtra" , ctypes.c_int),
("hInstance" , wintypes.HANDLE),
("hIcon" , wintypes.HICON),
("hCursor" , wintypes.HANDLE),
("hbrBackground", wintypes.HBRUSH),
("lpszMenuName" , wintypes.LPCWSTR),
("lpszClassName", wintypes.LPCWSTR),
("hIconSm" , wintypes.HICON),]
class Win32Gui(object):
def __init__(self, Ex=True, Unicode = True):
self.Ex = Ex
if self.Ex:
if Unicode:
self.GetMessage = GetMessageW
self.CreateWindowEx = CreateWindowExW
self.RegisterClassEx = RegisterClassExW
self.GetModuleHandle = GetModuleHandleW
self.DispatchMessage = DispatchMessageW
else:
self.GetMessage = GetMessageA
self.CreateWindowEx = CreateWindowExA
self.RegisterClassEx = RegisterClassExA
self.GetModuleHandle = GetModuleHandleA
self.DispatchMessage = DispatchMessageA
else:
self.GetMessage = GetMessageW
self.CreateWindowEx = CreateWindowExW
self.RegisterClassEx = RegisterClassExW
self.GetModuleHandle = GetModuleHandleW
self.DispatchMessage = DispatchMessageW
pass
def RGB(self, r=0, g=0, b=0):
rgb = 0xff & (int(r) | int(g) << 8 | int(b) << 16)
return rgb
def GetLTRB(self, titlename):
hwnd = win32gui.FindWindow(0, titlename)
left, top, right, bottom = win32gui.GetWindowRect(hwnd)
return left, top, right, bottom
def GetDesktopHW(self):
w = win32api.GetSystemMetrics(win32con.SM_CXSCREEN)
h = win32api.GetSystemMetrics(win32con.SM_CYSCREEN)
return h, w
def GetModuleHandleX(self):
if self.Ex:
hInstance = self.GetModuleHandle(0)
else:
hInstance = win32api.GetModuleHandle()
return hInstance
def GetWndClassEx(self, hInstance, WndProc, szWindowClass='DesktopApp', Transparent=True):
WndClassEx = WNDCLASSEX()
WndClassEx.cbSize = ctypes.sizeof(WNDCLASSEX)
WndClassEx.style = win32con.CS_HREDRAW | win32con.CS_VREDRAW
WndClassEx.lpfnWndProc = WNDPROCTYPE(WndProc)
WndClassEx.cbClsExtra = 0
WndClassEx.cbWndExtra = 0
WndClassEx.hInstance = hInstance
WndClassEx.hIcon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION)
WndClassEx.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW)
if Transparent:
WndClassEx.hbrBackground = self.RGB(0,0,0)
else:
WndClassEx.hbrBackground = win32gui.GetStockObject(win32con.WHITE_BRUSH)
WndClassEx.lpszMenuName = None
WndClassEx.lpszClassName = szWindowClass
WndClassEx.hIconSm = win32gui.LoadIcon(0, win32con.IDI_APPLICATION)
return WndClassEx
def GetWndClasss(self, hInstance, WndProc, szWindowClass='DesktopApp', Transparent=True):
WndClass = win32gui.WNDCLASS()
WndClass.style = win32con.CS_HREDRAW | win32con.CS_VREDRAW
WndClass.lpfnWndProc = WndProc
WndClass.hInstance = hInstance
WndClass.hIcon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION)
WndClass.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW)
if Transparent:
WndClass.hbrBackground = self.RGB(0,0,0)
else:
WndClass.hbrBackground = win32gui.GetStockObject(win32con.WHITE_BRUSH)
WndClass.lpszClassName = szWindowClass
return WndClass
def GetWndClassX(self, hInstance, WndProc, szWindowClass='DesktopApp', Transparent=True):
if self.Ex:
WndClass = self.GetWndClassEx(hInstance, WndProc, szWindowClass, Transparent=Transparent)
else:
WndClass = self.GetWndClasss(hInstance, WndProc, szWindowClass, Transparent=Transparent)
return WndClass
def RegisterClassX(self, WndClass):
try:
if self.Ex:
WndClassAtom = self.RegisterClassEx(ctypes.byref(WndClass))
else:
WndClassAtom = win32gui.RegisterClass(WndClass)
except Exception as e:
WndClassAtom = None
print(e)
raise e
return WndClassAtom
def CreateWindowX(self, WndClass, WinGuiClassAtom, szTitle, Transparent=True):
hight, width = self.GetDesktopHW()
if self.Ex:
if Transparent:
# hWindow = self.CreateWindowEx(
# win32con.WS_EX_TOPMOST | win32con.WS_EX_TRANSPARENT | win32con.WS_EX_LAYERED,
# WndClass.lpszClassName,
# szTitle,
# win32con.WS_POPUP,
# 0,
# 0,
# width,
# hight,
# 0,
# 0,
# WndClass.hInstance,
# None)
hWindow = self.CreateWindowEx(
win32con.WS_EX_TOPMOST | win32con.WS_EX_TRANSPARENT | win32con.WS_EX_LAYERED,
WndClass.lpszClassName,
szTitle,
win32con.WS_POPUP,
0,
0,
width,
hight,
0,
0,
WndClass.hInstance,
None)
else:
hWindow = self.CreateWindowEx(
win32con.WS_EX_TOPMOST,
WndClass.lpszClassName,
szTitle,
win32con.WS_OVERLAPPEDWINDOW, # win32con.WS_OVERLAPPEDWINDOW | win32con.WS_CAPTION
0,
0,
width,
hight,
0,
0,
WndClass.hInstance,
None)
else:
if Transparent:
hWindow = win32gui.CreateWindow(
WinGuiClassAtom, #it seems message dispatching only works with the atom, not the class name
szTitle,
win32con.WS_OVERLAPPEDWINDOW,
0,
0,
width,
hight,
0,
0,
WndClass.hInstance,
None)
else:
hWindow = win32gui.CreateWindow(
WinGuiClassAtom, #it seems message dispatching only works with the atom, not the class name
szTitle,
win32con.WS_OVERLAPPEDWINDOW,
win32con.CW_USEDEFAULT,
win32con.CW_USEDEFAULT,
win32con.CW_USEDEFAULT,
win32con.CW_USEDEFAULT,
0,
0,
WndClass.hInstance,
None)
# hWindow = win32gui.CreateWindow(
# WinGuiClassAtom, #it seems message dispatching only works with the atom, not the class name
# szTitle,
# win32con.WS_POPUP,
# 0,
# 0,
# width,
# hight,
# 0,
# 0,
# WndClass.hInstance,
# None)
return hWindow
def WindowMainLoopX(self, hWindow, Transparent=True):
# Show & update the window
if Transparent:
rgb = self.RGB(0, 0, 0)
SetLayeredWindowAttributes(hWindow, 0, rgb, win32con.LWA_COLORKEY)
win32gui.ShowWindow(hWindow, win32con.SW_SHOWNORMAL)
win32gui.UpdateWindow(hWindow)
lpmsg = ctypes.pointer(wintypes.MSG())
while self.GetMessage(lpmsg, 0, 0, 0) != 0:
# Dispatch messages
# win32gui.PumpMessages()
ctypes.windll.user32.TranslateMessage(lpmsg)
self.DispatchMessage(lpmsg)
# 使辅助窗口一直盖在游戏窗口上
# ctypes.windll.user32.MoveWindow(hWindow, 0, 23, width, hight-23, True)
# win32gui.CloseWindow(hWindow)
# time.sleep(1)
return None
def CreateGUi(self, WndProc=None, szTitle='Hello, World!', Transparent=True):
hInstance = self.GetModuleHandleX()
WndClass = self.GetWndClassX(hInstance, WndProc, Transparent=Transparent)
WndClassAtom = self.RegisterClassX(WndClass)
hWindow = self.CreateWindowX(WndClass, WndClassAtom, szTitle, Transparent=Transparent)
if hWindow:
self.WindowMainLoopX(hWindow, Transparent=Transparent)
return hWindow, hInstance, WndClass
pass
if __name__ == "__main__":
def Render(hWnd):
hDC, paintStruct = win32gui.BeginPaint(hWnd)
rect = win32gui.GetClientRect(hWnd)
win32gui.DrawText(
hDC,
'Hello send by Python via Win32!',
-1,
rect,
win32con.DT_SINGLELINE | win32con.DT_CENTER | win32con.DT_VCENTER)
win32gui.EndPaint(hWnd, paintStruct)
pass
def WndProc(hWnd, message, wParam, lParam):
if message == win32con.WM_PAINT:
Render(hWnd)
return 0
elif message == win32con.WM_CREATE:
return 0
elif message == win32con.WM_DESTROY:
print('Being destroyed')
win32gui.PostQuitMessage(0)
return 0
else:
return win32gui.DefWindowProc(hWnd, message, wParam, lParam)
Instance = Win32Gui(True)
Instance.CreateGUi(WndProc, Transparent=True)