forked from mmikeww/AHKv2-Gdip
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Gdip_Toolbox.ahk
247 lines (197 loc) · 6.85 KB
/
Gdip_Toolbox.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
#include Gdip_All.ahk
; Creates always on top window with current screen making impression as if screen froze
; Argument 'shouldFreeze' value 1: freeze, 0: unfreeze, -1: unfreeze all (if frozen more than once)
ScreenFreeze(shouldFreeze:=1) {
static freezeCounter:=0, frozenWindow:=""
static raster := 0x40000000 + 0x00CC0020 ; to capture layered windows too
switch shouldFreeze {
case -2: ; Unfreeze everything
if (freezeCounter > 0) {
frozenWindow.Hide()
}
freezeCounter := 0
return
case -1: ; Refresh frozen picture
if (freezeCounter < 1) {
return
}
frozenWindow.Hide()
case 0: ; Unfreeze one level
if (freezeCounter < 1) {
return
}
freezeCounter -= 1
if (freezeCounter < 1) {
frozenWindow.Hide()
}
return
case 1: ; Increase freeze level
freezeCounter += 1
if (freezeCounter > 1) {
return
}
default:
throw Error("Wrong argument value: " shouldFreeze)
}
pToken := Gdip_Startup()
pBitmap := Gdip_BitmapFromScreen(0 "|" 0 "|" A_ScreenWidth "|" A_ScreenHeight, raster)
if (!frozenWindow) {
frozenWindow := Gui("-Caption +E0x80000 +LastFound +AlwaysOnTop +ToolWindow +OwnDialogs")
}
hbm := Gdip_CreateHBITMAPFromBitmap(pBitmap)
Gdip_DisposeImage(pBitmap)
hdc := CreateCompatibleDC()
obm := SelectObject(hdc, hbm)
frozenWindow.Show("NA")
UpdateLayeredWindow(frozenWindow.hwnd, hdc, 0, 0, A_ScreenWidth, A_ScreenHeight)
SelectObject(hdc, obm)
DeleteObject(hbm)
DeleteDC(hdc)
Gdip_Shutdown(pToken)
frozenWindow.Opt("+OwnDialogs")
return frozenWindow
}
ScreenUnfreeze(shouldFreeze:=0) {
return ScreenFreeze(shouldFreeze)
}
; Draws colored rectangle (with optional transparency)
; Use `returnValue.Destroy()` to destroy the rectangle (AHK Gui object)
DrawRectangle(x1, y1, x2, y2, color, alpha:=1, &parentWindow:="") {
if (parentWindow && !parentWindow.HasProp("hwnd")) {
throw Error("The 'parentWindow' argument needs to have proper 'hwnd' property (for example a Gui object has it). Skip this value to create one.")
}
pToken := Gdip_Startup()
w := x2 - x1 + 1
h := y2 - y1 + 1
if (!parentWindow) {
parentWindow := Gui("-Caption +E0x80000 +LastFound +AlwaysOnTop +ToolWindow +OwnDialogs")
parentWindow.Show("NA")
}
hdc := CreateCompatibleDC()
hbm := CreateDIBSection(w, h)
obm := SelectObject(hdc, hbm)
G := Gdip_GraphicsFromHDC(hdc)
pBrush := Gdip_BrushCreateSolid((floor(alpha * 0xFF) << 24) | color)
Gdip_FillRectangle(G, pBrush, 0, 0, w, h)
Gdip_DeleteBrush(pBrush)
UpdateLayeredWindow(parentWindow.hwnd, hdc, x1, y1, w, h)
SelectObject(hdc, obm)
DeleteObject(hbm)
DeleteDC(hdc)
Gdip_DeleteGraphics(G)
Gdip_Shutdown(pToken)
return parentWindow
}
; Draws colored rectangle (with optional transparency)
; Use `returnValue.Destroy()` to destroy the rectangle (AHK Gui object)
DrawCircle(x, y, radius, color, alpha:=1, &parentWindow:="") {
if (parentWindow && !parentWindow.HasProp("hwnd")) {
throw Error("The 'parentWindow' argument needs to have proper 'hwnd' property (for example a Gui object has it). Skip this value to create one.")
}
pToken := Gdip_Startup()
x1 := x - radius + 1
y1 := y - radius + 1
w := radius * 2 - 1
h := radius * 2 - 1
if (!parentWindow) {
parentWindow := Gui("-Caption +Disabled +E0x80000 -LastFound +AlwaysOnTop +ToolWindow -OwnDialogs")
parentWindow.Show("NA")
}
hdc := CreateCompatibleDC()
hbm := CreateDIBSection(w, h)
obm := SelectObject(hdc, hbm)
G := Gdip_GraphicsFromHDC(hdc)
pBrush := Gdip_BrushCreateSolid((floor(alpha * 0xFF) << 24) | color)
; pBrush := Gdip_BrushCreateHatch((floor(alpha * 0xFF) << 24) | color, 0x7F1155FF, 4) ; Gdip_BrushCreateHatch(ARGBfront, ARGBback, HatchStyle:=0)
Gdip_FillEllipse(G, pBrush, 0, 0, w, h)
; Gdip_FillRectangle(G, pBrush, 0, 0, w, h)
Gdip_DeleteBrush(pBrush)
UpdateLayeredWindow(parentWindow.hwnd, hdc, x1, y1, w, h)
SelectObject(hdc, obm)
DeleteObject(hbm)
DeleteDC(hdc)
Gdip_DeleteGraphics(G)
Gdip_Shutdown(pToken)
return parentWindow
}
DrawTemporaryCircle(x, y, radius, color, alpha, time) {
circleWindow := DrawCircle(x, y, radius, color, alpha)
SetTimer(DestroyCircle, -time)
DestroyCircle() {
circleWindow.Destroy()
}
}
; Draws colored rectangle (with optional transparency), a slow version without Gdip use
; Use `returnValue.Destroy()` to destroy the rectangle (AHK Gui object)
DrawRectangleSlow(x1, y1, x2, y2, color, alpha:=1) {
global CoordText
w := x2 - x1 + 1
h := y2 - y1 + 1
myGui := Gui("-Caption +E0x80000 +AlwaysOnTop +ToolWindow -DPIScale +LastFound +OwnDialogs")
myGui.BackColor := color
WinSetTransparent(round(255*alpha), myGui)
myGui.Show()
myGui.Move(x1, y1, w, h)
return myGui
}
; Gets pixel color using screen buffer which refreshes on specified time period (150 ms by default)
GetPixelColorBuffered(x, y, refreshRate:=150, waitForUpdate:=false) {
static isBufferReady:=false, myDC:=0, screenX, screenY
static lastUpdateTick := 0
timeRemaining := lastUpdateTick + refreshRate - A_TickCount
if (timeRemaining <= 0 || waitForUpdate) {
if (waitForUpdate && timeRemaining > 0) {
sleep timeRemaining
}
UpdateScreenBuffer(0)
lastUpdateTick := A_TickCount
}
return GetPixelColorFromBuffer()
GetPixelColorFromBuffer() {
if (!IsInteger(x) || !IsInteger(y)) {
throw Error("Wrong parameter types: x=" x ", y=" y)
}
; check if there is a valid data buffer
if (!isBufferReady) {
Start := A_TickCount
while (!isBufferReady) {
Sleep 10
if (A_TickCount - Start > 5000) { ; time out if data is not ready after 5 seconds
return -3
}
}
}
return DllCall("GetPixel", "Uint", myDC, "int", x - screenX, "int", y - screenY)
}
; Default window handle is NULL (entire screen)
UpdateScreenBuffer(windowHandle:=0) {
static oldObject := 0, hBuffer := 0
static screenWOld := 0, screenHOld := 0
; get screen dimensions
screenX := SysGet(76)
screenY := SysGet(77)
screenW := SysGet(78)
screenH := SysGet(79)
isBufferReady := 0
; determine whether the old buffer can be reused
bufferInvalid := screenW != screenWOld || screenH != screenHOld || myDC == 0 || hBuffer == 0
screenWOld := screenW
screenHOld := screenH
if (bufferInvalid) {
; cleanly discard the old buffer
DllCall("SelectObject", "Uint", myDC, "Uint", oldObject)
DllCall("DeleteDC", "Uint", myDC)
DllCall("DeleteObject", "Uint", hBuffer)
; create a new empty buffer
myDC := CreateCompatibleDC(0)
hBuffer := CreateDIBSection(screenW, screenH, myDC)
oldObject := DllCall("SelectObject", "Uint", myDC, "Uint", hBuffer)
}
screenDC := DllCall("GetDC", "Uint", windowHandle)
; retrieve the whole screen into the newly created buffer
DllCall("BitBlt", "Uint", myDC, "int", 0, "int", 0, "int", screenW, "int", screenH, "Uint", screenDC, "int", screenX, "int", screenY, "Uint", 0x40000000 | 0x00CC0020)
; important: release the DC of the screen
DllCall("ReleaseDC", "Uint", windowHandle, "Uint", screenDC)
isBufferReady := 1
}
}