-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDPaZ.ahk
309 lines (257 loc) · 9.37 KB
/
DPaZ.ahk
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
#Requires AutoHotkey v2.0
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; A script which lets you move stuff around the screen, resize it,
;; and pan and zoom around your whole desktop, just like you would in
;; something like Miro
;;
;; Press ctrl+windows then mouse buttons:
;; Left mouse: Move the window under the cursor
;; Right mouse: Resize the window under the cursor
;; Middle mouse: Pan the whole desktop around
;; Scroll wheel: "Zoom" the desktop - feels like you're zooming into or
;; out of the desktop; in practice, it's simply resizing
;; and moving the windows about, sometimes to locations
;; outside of the current screen resolution
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
DetectHiddenWindows false
global StartX := 0
global StartY := 0
global delay := 10
global ZoomAmount := 0.30
global Busy := false
;; the last window we "grabbed" - so we don't accidentally grab another one during move/resize
global GrabbedWindow := ""
;; whether we're in the right/left, top/bottom half of the window (for deciding which direction to resize)
;; either "left"/"right"/""
global GrabbedHalfX := ""
;; either "top"/"bottom"/""
global GrabbedHalfY := ""
coordmode "Mouse", "Screen"
A_MaxHotkeysPerInterval := 1000
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; HOTKEYS
; Hotkey for starting window pan
#^MButton:: {
global StartX
global StartY
MouseGetPos &StartX, &StartY
SetTimer PanWindows, -delay
}
#^MButton Up:: {
SetTimer PanWindows, 0
}
#^LButton:: {
global StartX
global StartY
MouseGetPos &StartX, &StartY
SetTimer MoveWindow, -delay
}
#^LButton Up:: {
global GrabbedWindow := ""
SetTimer MoveWindow, 0
}
#^RButton:: {
global StartX
global StartY
MouseGetPos &StartX, &StartY
SetTimer ResizeWindow, -delay
}
#^RButton Up::{
global GrabbedWindow := ""
global GrabbedHalfX := ""
global GrabbedHalfY := ""
SetTimer ResizeWindow, 0
}
#^WheelDown::ZoomWindows(-1)
#^WheelUp::ZoomWindows(1)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ACTIONS
ZoomWindows(zoom) {
global StartX
global StartY
MouseGetPos &StartX, &StartY
DoStuffToWindows("zoom", zoom)
GuiHwnd := WinExist()
}
PanWindows() {
DoStuffToWindows("pan", 1)
}
MoveWindow() {
DoStuffToWindows("move", 1)
}
ResizeWindow() {
DoStuffToWindows("resize", 1)
}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; MAGIC
DoStuffToWindows(what, multiplier) {
SetWinDelay -1
global StartX
global StartY
global Busy
global GrabbedWindow
global GrabbedHalfX
global GrabbedHalfY
switch what {
case "move":
if not GetKeyState("LButton", "P")
return
case "pan":
if not GetKeyState("MButton", "P")
return
case "resize":
if not GetKeyState("RButton", "P")
return
}
if (MouseClickDrag)
;; if we're already doing something to the window, don't try and clobber it
if (Busy)
return
Busy := true
; Get the current mouse position
MouseGetPos &MouseX, &MouseY, &WindowUnderCursor
; Calculate the offset
OffsetX := MouseX - StartX
OffsetY := MouseY - StartY
;; Get the list of window ids to affect; different actions have different criteria
WindowIds := []
if (what == "resize" || what == "move") {
; only applies to window under the cursor
if (not GrabbedWindow)
GrabbedWindow := WindowUnderCursor
WindowIds := [GrabbedWindow]
}
else {
;; applying to all windows (zoom and pan)
; WindowIds := WinGetList("Notepad") ; for testing
WindowIds := WinGetList(,, "Program Manager")
}
;; remove "bad" windows from our list - either removed windows, or windows that are too small, minimised, hidden etc
NewWindowIds := []
for window in WindowIds {
;; window has been removed since we started the loop?
if not WinExist(window)
continue
WinGetPos(&WindowX, &WindowY, &WindowWidth, &WindowHeight, window)
if ( not IsGoodWindow(window, WindowWidth, WindowHeight))
continue
NewWindowIds.Push(window)
}
;; Loop through the windows and "do stuff" to them
for window in NewWindowIds {
WinGetPos(&WindowX, &WindowY, &WindowWidth, &WindowHeight, window)
;; Otherwise, we're good to go
if (what == "zoom") {
; apply a static zoom on each iteration
scale := 1 + (multiplier * ZoomAmount)
; then get a vector from current mouse pos to the center of the window
; now move the center of the window to the end point of the scaled vector
global Center := [WindowWidth / 2, WindowHeight / 2]
global Vector := [(WindowX + Center[1]) - MouseX, (WindowY + Center[2]) - MouseY]
; scale the vector, but keep the origin the same
global Scaled := [Vector[1] * scale, Vector[2] * scale]
global NewPos := [MouseX + Scaled[1] - (Center[1] * scale), MouseY + Scaled[2] - (Center[2] * scale)]
MoveAndResize(NewPos[1], NewPos[2], WindowWidth * scale, WindowHeight * scale, window)
}
else if (what == "resize") {
;; figure out which quarter of the window we're in so we can "drag" that corner
;; essentially moves the window minus amount, and increases its size in the opposite direction
;; so the opposite corner doesn't move
if (not GrabbedHalfX || not GrabbedHalfY) {
GrabbedHalfX := "right"
GrabbedHalfY := "bottom"
if (MouseX < WindowX + WindowWidth / 2)
GrabbedHalfX := "left"
if (MouseY < WindowY + WindowHeight / 2)
GrabbedHalfY := "top"
}
if (GrabbedHalfX == "left") {
OffsetX *= -1
WindowX -= OffsetX
}
if (GrabbedHalfY == "top") {
OffsetY *= -1
WindowY -= OffsetY
}
MoveAndResize(WindowX, WindowY, WindowWidth + OffsetX, WindowHeight + OffsetY, window)
}
else if (what == "move" || what == "pan") {
MoveAndResize((WindowX + OffsetX), (WindowY + OffsetY), , , window)
}
}
for window in NewWindowIds {
SetTimer(ResumeRedraw.Bind(window), -delay)
}
; Update the start position for the next calculation
StartX := MouseX
StartY := MouseY
if (what == "pan")
SetTimer PanWindows, -delay
if (what == "move")
SetTimer MoveWindow, -delay
if (what == "resize")
SetTimer ResizeWindow, -delay
Busy := false
}
PauseRedraw(window) {
; SendMessage(0xB, 0, 0,, window) ; wParam 0 disables redraw
}
ResumeRedraw(window) {
SendMessage(0xB, 1, 0,, window) ; wParam 1 enables redraw
; force a redraw
DllCall("RedrawWindow", "Ptr", WinExist(window), "Ptr", 0, "Ptr", 0, "UInt", 0x85)
}
IsGoodWindow(window, WindowWidth, WindowHeight) {
;; Skip tiny windows
if (WindowWidth < 2 || WindowHeight < 2)
return false
;; Skip disabled or minimized windows
Style := WinGetStyle(window)
WS_DISABLED := 0x8000000
WS_MINIMIZE := 0x20000000
if (Style & WS_DISABLED || Style & WS_MINIMIZE) {
return false
}
;; Skip fully transparent windows
Trans := WinGetTransparent(window)
if (Trans == 0)
return false
;; Skip windows with no title
Title := WinGetTitle(window)
if (Title == "" || Title == "Transparent Window")
return false
return true
}
MoveAndResize(WindowX, WindowY, WindowWidth := "", WindowHeight := "", Window := "") {
SWP_NOREDRAW := 0x0008
SWP_NOSENDCHANGING := 0x0400
SWP_DEFERERASE := 0x2000
SWP_NOCOPYBITS := 0x0100
SWP_NOZORDER := 0x0004
Flags := SWP_NOREDRAW | SWP_NOSENDCHANGING | SWP_DEFERERASE | SWP_NOCOPYBITS | SWP_NOZORDER
try {
if (WindowWidth && WindowHeight) {
; SWP_gcc
DllCall("SetWindowPos", "UInt", Window, "UInt", 0, "Int", WindowX, "Int", WindowY, "Int", WindowWidth, "Int", WindowHeight, "UInt", Flags)
; WinMove(WindowX, WindowY, WindowWidth, WindowHeight, window)
}
else {
SWP_NOSIZE := 0x0001
Flags |= SWP_NOSIZE
DllCall("SetWindowPos", "UInt", Window, "UInt", 0, "Int", WindowX, "Int", WindowY, "Int", 0, "Int", 0, "UInt", Flags)
; WinMove(WindowX, WindowY, , , Window)
}
}
catch {
;; not a lot we can do here...
}
}
Canvas_DrawLine(p_x1, p_y1, p_x2, p_y2) {
hDC := DllCall("GetDC", "UInt", 0)
hCurrPen := DllCall("CreatePen", "UInt", 0, "UInt", 2, "UInt", 0xff0000)
DllCall("SelectObject", "UInt", hdc, "UInt", hCurrPen)
DllCall("gdi32.dll\MoveToEx", "UInt", hdc, "UInt", p_x1, "UInt", p_y1, "UInt", 0)
DllCall("gdi32.dll\LineTo", "UInt", hdc, "UInt", p_x2, "UInt", p_y2)
DllCall("ReleaseDC", "uint", 0, "uint", hDC)
DllCall("DeleteObject", "UInt", hCurrPen)
}