-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwinSpy.ahk2
342 lines (304 loc) · 10.5 KB
/
winSpy.ahk2
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
;
; Window Spy
;
#Requires AutoHotkey v2.0
#NoTrayIcon
#SingleInstance Ignore
SetWorkingDir(A_ScriptDir)
CoordMode("Pixel", "Screen")
WinGetTextFast(window, detect_hidden) {
; WinGetText ALWAYS uses the "fast" mode - TitleMatchMode only affects
; WinText/ExcludeText parameters. In Slow mode, GetWindowText() is used
; to retrieve the text of each control.
try {
controls := WinGetControlsHwnd(window)
} catch TargetError as e {
return "Get controls fail: " e.Message
}
static WINDOW_TEXT_SIZE := 32767 ; Defined in AutoHotkey source.
buf := Buffer(WINDOW_TEXT_SIZE * 2) ; *2 for Unicode
local text := ""
for control in controls {
if !detect_hidden && !DllCall("IsWindowVisible", "ptr", control)
continue
if !DllCall("GetWindowText", "ptr", control, "ptr", buf, "int", WINDOW_TEXT_SIZE)
continue
text .= StrGet(buf) . "`r`n"
}
return text
}
ScreenToClientPos(hWnd, &x, &y) {
try {
WinGetPos(&wX, &wY, , , hWnd)
} catch TargetError {
return false
}
x += wX
y += wY
pt := Buffer(8)
NumPut("int", x, "int", y, pt)
if !DllCall("ScreenToClient", "ptr", hWnd, "ptr", pt)
return false
x := NumGet(pt, 0, "int")
y := NumGet(pt, 4, "int")
return true
}
textMangle(text) {
local elli := false
if (pos := InStr(text, "`n")) {
text := SubStr(text, 1, pos - 1)
elli := true
}
if (StrLen(text) > 40) {
text := SubStr(text, 1, 40)
elli := true
}
if (elli) text .= " …"
return text
}
GetMouseInfo() {
CoordMode("Mouse", "Screen")
MouseGetPos(&msX, &msY)
CoordMode "Mouse", "Window"
MouseGetPos(&mrX, &mrY)
CoordMode "Mouse", "Client"
MouseGetPos(&mcX, &mcY)
local pixelColor := PixelGetColor(msX, msY, "RGB")
return (
"Screen:`t" msX ", " msY " (less often used)`n"
"Window:`t" mrX ", " mrY " (default)`n"
"Client:`t" mcX ", " mcY " (recommended)`n"
"Color:`t#" SubStr(pixelColor, 3)
)
}
GetWindowInfo(window) {
try {
return (
WinGetTitle(window) "`n"
"ahk_class " WinGetClass(window) "`n"
"ahk_exe " WinGetProcessName(window) "`n"
"ahk_pid " WinGetPID(window)
)
} catch TargetError as e {
return "Get window info fail: " e.Message
}
}
GetWindowPosInfo(window) {
try {
WinGetPos(&wX, &wY, &wW, &wH, window)
WinGetClientPos(&wcX, &wcY, &wcW, &wcH, window)
return (
"`tX: " wX
"`tY: " wY
"`tW: " wW
"`tH: " wH
"`nClient:`tX: " wcX "`tY: " wcY "`tW: " wcW "`tH: " wcH
)
} catch TargetError as e {
return "Get window position fail: " e.Message
}
}
GetStatusBarText(window) {
text := ""
loop {
try {
text .= "[" A_Index "]`t" textMangle(StatusBarGetText(A_Index, window)) "`n"
} catch as e {
break
}
}
return SubStr(text, 1, -1)
}
GetControlInfo(window, control) {
try {
ControlGetPos(&screenX, &screenY, &screenWidth, &screenHeight, control)
clientX := screenX
clientY := screenY
ScreenToClientPos(window, &clientX, &clientY)
WinGetClientPos(, , &clientWidth, &clientHeight, ControlGetHwnd(control))
return (
"ClassNN:`t" ControlGetClassNN(control) "`n"
"Text:`t" textMangle(ControlGetText(control)) "`n"
"Screen:`tX: " screenX "`tY: " screenY "`tW: " screenWidth "`tH: " screenHeight "`n"
"Client:`tX: " clientX "`tY: " clientY "`tW: " clientWidth "`tH: " clientHeight
)
} catch TargetError as e {
return "Get control info fail: " e.Message
}
}
GetVisibleText(window, slowMode) {
if (slowMode) {
try {
DetectHiddenText(False)
return WinGetText(window)
} catch TargetError as e {
return "Get visible text fail: " e.Message
}
} else {
return WinGetTextFast(window, false)
}
}
GetAllText(window, slowMode) {
if (slowMode) {
try {
DetectHiddenText(True)
return WinGetText(window)
} catch TargetError as e {
return "Get text fail: " e.Message
}
} else {
return WinGetTextFast(window, true)
}
}
class MainWindow { ; Singleton
window := 0
control := 0
textCache := Map()
autoUpdateEnabled := false
textList := Map(
"NotFrozen", "Updating...",
"Frozen", "Update suspended",
"MouseCtrl", "Control Under Mouse Position",
"FocusCtrl", "Focused Control",
)
OnOptionUpdateChange(*) {
this.updateAutoUpdateTimer()
}
OnOptionAlwaysOnTopChanged(checkbox, *) {
this.gui.opt(
(checkbox.value ? "+" : "-")
"AlwaysOnTop"
)
}
OnOptionTargetChange(*) {
this.SetText(
"CtrlLabel",
this.textList[this.gui["GetCursor"].value ? "MouseCtrl" : "FocusCtrl"] ":"
)
}
OnResize(window, minMax, width, height) {
; Stop auto update when minimize
if (minMax == -1) {
this.autoUpdate(false)
} else {
this.autoUpdate(true)
}
list := "Title,MousePos,Ctrl,Pos,SBText,VisText,AllText,Options"
loop parse list, "," {
window[A_LoopField].move(, , width - window.marginX * 2)
}
}
OnClose(window) {
ExitApp
}
__New() {
this.gui := Gui("+AlwaysOnTop +Resize +DPIScale MinSize", "Window Spy")
this.gui.add("Text", "xm", "Window Title, Class and Process:")
this.gui.add("Edit", "xm w320 r4 ReadOnly -Wrap vTitle")
this.gui.add("Text", , "Mouse Position:")
this.gui.add("Edit", "w320 r4 ReadOnly -Wrap vMousePos")
this.gui.add("Text", "w320 vCtrlLabel", this.textList["FocusCtrl"] ":")
this.gui.add("Edit", "w320 r4 ReadOnly -Wrap vCtrl")
this.gui.add("Text", , "Active Window Position:")
this.gui.add("Edit", "w320 r2 ReadOnly -Wrap vPos")
this.gui.add("Text", , "Status Bar Text:")
this.gui.add("Edit", "w320 r2 ReadOnly -Wrap vSBText")
this.gui.add("Checkbox", "vIsSlow", "Slow TitleMatchMode")
this.gui.add("Text", , "Visible Text:")
this.gui.add("Edit", "w320 r2 ReadOnly -Wrap vVisText")
this.gui.add("Text", , "All Text:")
this.gui.add("Edit", "w320 r2 ReadOnly -Wrap vAllText")
this.gui.add("GroupBox", "w320 r3 vOptions", "Options")
this.gui.add("Checkbox", "xm+8 yp+16 vAlwaysOnTop checked", "Always on top")
.OnEvent("Click", ObjBindMethod(this, "OnOptionAlwaysOnTopChanged"))
this.gui.add("Text", "xm+8 y+m", "Update when Ctrl key is")
this.gui.add("Radio", "yp vUpdateWhenCtrlUp checked", "up")
.OnEvent("Click", ObjBindMethod(this, "OnOptionUpdateChange"))
this.gui.add("Radio", "yp vUpdateWhenCtrlDown", "down")
.OnEvent("Click", ObjBindMethod(this, "OnOptionUpdateChange"))
this.gui.add("Text", "xm+8 y+m", "Get info of")
this.gui.add("Radio", "yp vGetActive checked", "Active window")
.OnEvent("Click", ObjBindMethod(this, "OnOptionTargetChange"))
this.gui.add("Radio", "yp vGetCursor", "Window on cursor")
.OnEvent("Click", ObjBindMethod(this, "OnOptionTargetChange"))
; Update label once
this.OnOptionTargetChange()
this.statusBar := this.gui.add("StatusBar", , this.textList["NotFrozen"])
this.gui.OnEvent("Size", ObjBindMethod(this, "OnResize"))
this.gui.OnEvent("Close", ObjBindMethod(this, "OnClose"))
this.OnUpdate := ObjBindMethod(this, "Update")
}
SetText(controlID, text) {
; Unlike using a pure GuiControl, this function causes the text of the
; controls to be updated only when the text has changed, preventing periodic
; flickering (especially on older systems).
if (!this.textCache.has(controlID) || this.textCache[controlID] != text) {
this.textCache[controlID] := text
this.gui[controlID].value := text
}
}
UpdateTarget() {
if this.gui["GetCursor"].value {
MouseGetPos(, , &window, &control, 2)
this.window := window
this.control := control
} else {
this.window := WinExist("A")
try {
this.control := ControlGetFocus()
} catch TargetError {
this.control := 0
}
}
}
Update() {
this.UpdateTarget()
; Our Gui || Alt-tab
try {
if (this.window = this.gui.Hwnd || WinGetClass() = "MultitaskingViewFrame") {
this.statusBar.SetText(this.textList["Frozen"])
return
}
} catch TargetError {
}
if (this.window && this.control) {
this.SetText("Ctrl", GetControlInfo(this.window, this.control))
} else {
this.SetText("Ctrl", "")
}
this.statusBar.SetText(this.textList["NotFrozen"])
this.SetText("Title", GetWindowInfo(this.window))
this.SetText("MousePos", GetMouseInfo())
this.SetText("Pos", GetWindowPosInfo(this.window))
this.SetText("SBText", GetStatusBarText(this.window))
this.SetText("VisText", GetVisibleText(this.window, this.gui["IsSlow"].Value))
this.SetText("AllText", GetAllText(this.window, this.gui["IsSlow"].Value))
}
autoUpdate(enable) {
if (enable == this.autoUpdateEnabled) {
return
}
if (enable) {
SetTimer(this.OnUpdate, 100)
} else {
SetTimer(this.OnUpdate, 0)
this.statusBar.SetText(this.textList["Frozen"])
}
this.autoUpdateEnabled := enable
}
updateAutoUpdateTimer() {
local ctrlKeyDown := GetKeyState("Ctrl", "P")
local enable := (
ctrlKeyDown == window.gui["UpdateWhenCtrlDown"].value
)
this.autoUpdate(enable)
}
}
global window := MainWindow()
window.gui.Show("NoActivate")
window.autoUpdate(true)
~*Ctrl::
~*Ctrl up:: {
global window
window.updateAutoUpdateTimer()
}