-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextlink.pas
More file actions
276 lines (252 loc) · 7.86 KB
/
extlink.pas
File metadata and controls
276 lines (252 loc) · 7.86 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
unit extlink;
interface
uses windows,sysutils,graphics,classes;
type
tLinkToExtWindow=class(tInterfacedObject)
private
ShadowRect:tRect;
ShadowBMP:tBitmap;
protected
WinHandle:tHandle;
ExtCanvas:tCanvas;
ScreenWorkCounter:integer;
procedure SaveRange(x,y:integer);
function SuitableName(Name:string):boolean;virtual;abstract;
procedure GetFromScreen(bmp: tBitmap;deltax,deltay:integer);
function DiffColors(color1,color2:tColor):Boolean;virtual;
function SameShapeOnCanvas(bmp: tBitmap;canvas:tCanvas;deltax,deltay:integer):boolean;
function SameBMPOnCanvas(bmp: tBitmap;canvas:tCanvas;deltax,deltay:integer):boolean;
function SameShapeOnScreen(bmp: tBitmap;deltax,deltay:integer):boolean;
function FindBMPOnCanvas(bmp: tBitmap;canvas:tCanvas;var deltax,deltay:integer;RectToFind:tRect):boolean;
function FindBMPOnScreen(bmp: tBitmap;var deltax,deltay:integer;RectToFind:tRect):boolean;
function FindBMPFileOnScreen(fn:string;var deltax,deltay:integer;RectToFind:tRect):boolean;
procedure SaveToFile(Rect:tRect;fn:string);
procedure CreateShadowBMP(Rect:tRect);
procedure StartScreenWork;
procedure StopScreenWork;
public
constructor create;
destructor destroy;override;
procedure Init;virtual;
end;
implementation
uses Types;
{ tLinkToExtWindow }
constructor tLinkToExtWindow.create;
begin
ExtCanvas:=tCanvas.Create;
end;
destructor tLinkToExtWindow.destroy;
begin
ExtCanvas.Free;
inherited;
end;
function EnumFunc(h:tHandle;Param:tLinkToExtWindow):BOOL;stdcall;
var namebuf:array[0..1024] of char;
begin
Result:=True;
GetWindowText(h,@NameBuf,sizeof(namebuf)-1);
if Param.SuitableName(namebuf) then begin
Param.WinHandle:=h;
Result:=False;
end;
end;
procedure tLinkToExtWindow.SaveRange(x,y:integer);
var tmp:tBitmap;
begin
tmp:=tBitmap.create;
tmp.Width:=21;
tmp.height:=21;
tmp.canvas.CopyRect(rect(0,0,21,21),ExtCanvas,rect(x-10,y-10,x+10,y+10));
tmp.savetofile('zr.bmp');
tmp.free;
end;
procedure tLinkToExtWindow.GetFromScreen(bmp: tBitmap;deltax,deltay:integer);
begin
StartScreenWork;
try
bmp.canvas.CopyRect(rect(0,0,bmp.Width+1,bmp.Height+1),ExtCanvas,
rect(deltax,deltay,bmp.Width+1+deltax,bmp.Height+1+deltay));
finally
StopScreenWork;
end;
end;
procedure tLinkToExtWindow.Init;
begin
WinHandle:=0;
EnumWindows(@EnumFunc,integer(self));
if WinHandle=0 then
raise exception.Create('Window not found')
end;
function tLinkToExtWindow.FindBMPOnScreen(bmp: tBitmap; var deltax,
deltay: integer;RectToFind:tRect): boolean;
var
tmp:tBitmap;
tmpdeltax,
tmpdeltay: integer;
begin
StartScreenWork;
try
if (ShadowBMP<>nil) and (ShadowRect.Left<=RectToFind.Left) and
(ShadowRect.Top<=RectToFind.Top) and
(ShadowRect.Right>=RectToFind.Right) and
(ShadowRect.Bottom>=RectToFind.Bottom) then begin
OffsetRect(RectToFind,-ShadowRect.Left,-ShadowRect.Top);
Result:=FindBMPOnCanvas(bmp,ShadowBMP.canvas,tmpdeltax,tmpdeltay,RectToFind);
if Result then begin
Deltax:=tmpDeltax+ShadowRect.Left;
DeltaY:=TmpDeltaY+ShadowRect.Top;
end;
end else begin
tmp:=tBitmap.Create;
try
tmp.Height:=RectToFind.Bottom-RectToFind.Top;
tmp.Width:=RectToFind.Right-RectToFind.Left;
tmp.canvas.CopyRect(rect(0,0,tmp.Width,tmp.Height),ExtCanvas,
RectToFind);
// Result:=FindBMPOnCanvas(bmp,ExtCanvas,deltax,deltay,RectToFind);
Result:=FindBMPOnCanvas(bmp,tmp.canvas,tmpdeltax,tmpdeltay,rect(0,0,tmp.Width,tmp.Height));
if Result then begin
Deltax:=tmpDeltax+RectToFind.Left;
DeltaY:=TmpDeltaY+RectToFind.Top;
end;
finally
tmp.Free;
end;
end;
finally
StopScreenWork;
end;
end;
function tLinkToExtWindow.FindBMPFileOnScreen(fn: string; var deltax: integer;
var deltay: integer; RectToFind: tRect): boolean;
var bmp:tBitmap;
begin
bmp:=tBitmap.Create;
try
bmp.LoadFromFile(fn);
Result:=FindBMPOnScreen(bmp,deltax,deltay,RectToFind);
finally
bmp.Free;
end;
end;
function tLinkToExtWindow.FindBMPOnCanvas(bmp: tBitmap; canvas: tCanvas;
var deltax, deltay: integer; RectToFind: tRect): boolean;
begin
Result:=False;
deltay:=0;
while deltay<=RectToFind.Bottom-bmp.Height do begin
deltax:=0;
while deltax<=RectToFind.Right-bmp.Width do begin
if SameShapeOnCanvas(bmp,Canvas,deltax,deltay) then begin
Result:=True;
exit;
end;
inc(Deltax);
end;
inc(deltay);
end;
end;
function tLinkToExtWindow.SameShapeOnCanvas(bmp: tBitmap; canvas: tCanvas;
deltax, deltay: integer): boolean;
var
i,j:integer;
begin
Result:=True;
for i:=0 to bmp.width-1 do
for j:=0 to bmp.height-1 do
if DiffColors(Canvas.Pixels[deltax+i,deltay+j],bmp.Canvas.Pixels[i,j]) then begin
Result:=False;
exit;
end;
end;
function tLinkToExtWindow.SameBMPOnCanvas(bmp: tBitmap; canvas: tCanvas;
deltax, deltay: integer): boolean;
var
i,j:integer;
begin
Result:=True;
for i:=0 to bmp.width-1 do
for j:=0 to bmp.height-1 do
if Canvas.Pixels[deltax+i,deltay+j]<>bmp.Canvas.Pixels[i,j] then begin
Result:=False;
exit;
end;
end;
procedure tLinkToExtWindow.StartScreenWork;
var h:tHandle;
begin
if ScreenWorkCounter=0 then begin
h:=GetDCEx(WinHandle,0,DCX_LOCKWINDOWUPDATE or DCX_WINDOW {or DCX_CACHE});
if h=0 then
raise exception.Createfmt('GetDC error %d',[GetLastError]);
ExtCanvas.Handle:=h;
if ShadowBMP<>nil then
ShadowBMP.canvas.CopyRect(rect(0,0,ShadowBMP.Width,ShadowBMP.Height),ExtCanvas,
ShadowRect);
end;
inc(ScreenWorkCounter);
end;
procedure tLinkToExtWindow.StopScreenWork;
begin
dec(ScreenWorkCounter);
if ScreenWorkCounter=0 then begin
ReleaseDC(WinHandle,ExtCanvas.Handle);
ExtCanvas.Handle:=0;
end;
end;
function tLinkToExtWindow.SameShapeOnScreen(bmp: tBitmap; deltax,
deltay: integer): boolean;
begin
if bmp=nil then begin
Result:=False;
exit;
end;
StartScreenWork;
try
if (ShadowBMP<>nil) and (ShadowRect.Left<=deltax) and
(ShadowRect.Top<=deltay) and
(ShadowRect.Right>=deltax+bmp.width) and
(ShadowRect.Bottom>=deltay+bmp.Height) then
Result:=SameShapeOnCanvas(bmp,ShadowBMP.Canvas,deltax-ShadowRect.Left,deltay-ShadowRect.Top)
else
Result:=SameShapeOnCanvas(bmp,ExtCanvas,deltax,deltay);
finally
StopScreenWork;
end;
end;
procedure tLinkToExtWindow.CreateShadowBMP(Rect: tRect);
begin
ShadowRect:=Rect;
if (Rect.Left=Rect.Right) or (Rect.Top=Rect.Bottom) then
ShadowBMP.Free
else begin
if ShadowBMP=nil then
ShadowBMP:=tBitmap.Create;
ShadowBMP.Width:=rect.Right-rect.Left;
ShadowBMP.Height:=rect.Bottom-rect.Top;
end;
end;
function tLinkToExtWindow.DiffColors(color1, color2: tColor): Boolean;
begin
Result:=color1<>color2;
end;
procedure tLinkToExtWindow.SaveToFile(Rect: tRect; fn: string);
var image:tBitmap;
begin
StartScreenWork;
try
image:=tBitmap.Create;
try
image.Width:=rect.Right-rect.Left;
image.Height:=rect.Bottom-rect.Top;
GetFromScreen(image,rect.Left,rect.Top);
image.SaveToFile(fn);
finally
image.Free;
end;
finally
StopScreenWork;
end;
end;
end.