-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSystem.ClickRecorder.Screenshot.Provider.pas
More file actions
244 lines (206 loc) · 7.5 KB
/
System.ClickRecorder.Screenshot.Provider.pas
File metadata and controls
244 lines (206 loc) · 7.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
unit System.ClickRecorder.Screenshot.Provider;
interface
uses
{$IFDEF MSWINDOWS}
Vcl.Graphics,
Vcl.Forms,
Winapi.Windows,
{$ENDIF}
{$IFDEF MACOS}
FMX.Graphics
{$ENDIF}
System.SysUtils;
type
IClickRecorderScreenshotProvider = interface(IInvokable)
['{DB538C33-2820-4230-AC0B-54DC20D0EA79}']
function DrawCursor(ADrawCursor: Boolean): IClickRecorderScreenshotProvider; overload;
function DrawCursor: Boolean overload;
function Monitor(AMonitor: Integer): IClickRecorderScreenshotProvider; overload;
function Monitor(): Integer; overload;
function IsRecording: Boolean;
function TakeAScreenshot(APoint: TPoint): Vcl.Graphics.TBitmap;
end;
TClickRecorderScreenshotProvider = class(TInterfacedObject, IClickRecorderScreenshotProvider)
strict private
FIsRecording: Boolean;
FScreenShotDrawCursor: Boolean;
FScreenShotMonitor: Integer; // AMonitor: 0 = ALL screens, 1 = MAIN screen, 2 = ADDITIONAL screen
FMousePoint: TPoint;
private
procedure DrawScreenCursor(var ABMP: Vcl.Graphics.TBitmap; const AMonitorID: Integer);
function TakeScreenshot(var ABMP: Vcl.Graphics.TBitmap; AMonitorNumber: Integer): Boolean;
function ScreenShot(ABMP: Vcl.Graphics.TBitmap; const ADrawCursor: Boolean; const AAMonitor: Integer): Boolean;
public
constructor Create();
destructor Destroy; override;
function DrawCursor(ADrawCursor: Boolean): IClickRecorderScreenshotProvider; overload;
function DrawCursor: Boolean overload;
function Monitor(AMonitor: Integer): IClickRecorderScreenshotProvider; overload;
function Monitor(): Integer; overload;
function IsRecording: Boolean;
function TakeAScreenshot(APoint: TPoint): Vcl.Graphics.TBitmap;
end;
implementation
uses
System.IOUtils,
Winapi.Messages;
{ TClickRecorderScreenshotProvider }
constructor TClickRecorderScreenshotProvider.Create;
begin
inherited Create();
FScreenShotDrawCursor := True;
FScreenShotMonitor := 0;
end;
destructor TClickRecorderScreenshotProvider.Destroy;
begin
//...
inherited;
end;
function TClickRecorderScreenshotProvider.DrawCursor: Boolean;
begin
Result := FScreenShotDrawCursor;
end;
function TClickRecorderScreenshotProvider.DrawCursor(ADrawCursor: Boolean): IClickRecorderScreenshotProvider;
begin
FScreenShotDrawCursor := ADrawCursor;
Result := Self;
end;
function TClickRecorderScreenshotProvider.Monitor: Integer;
begin
Result := FScreenShotMonitor;
end;
function TClickRecorderScreenshotProvider.Monitor(AMonitor: Integer): IClickRecorderScreenshotProvider;
begin
FScreenShotMonitor := AMonitor;
Result := Self;
end;
procedure TClickRecorderScreenshotProvider.DrawScreenCursor(var ABMP: Vcl.Graphics.TBitmap; const AMonitorID: Integer);
var
CanvasClipRect: TRect;
CursorInfo: TCursorInfo;
Icon: TIcon;
IconInfo: TIconInfo;
Mon: TMonitor;
CPOffset: TPoint;
begin
// Mon := Screen.Monitors[AMonitorID];
Mon := Screen.MonitorFromPoint(FMousePoint); // Detect the monitor under mouse point...
CanvasClipRect := ABMP.Canvas.ClipRect;
Icon := TIcon.Create;
try
// MonitorID: 0=ALL screens, 1=MAIN screen, 2=ADDITIONAL screen, etc....
// https://docwiki.embarcadero.com/RADStudio/Florence/en/Handling_the_Screen
CursorInfo.cbSize := SizeOf(CursorInfo);
if GetCursorInfo(CursorInfo) then
begin
if (CursorInfo.Flags = CURSOR_SHOWING) then
begin
Icon.Handle := CopyIcon(CursorInfo.hCursor);
if GetIconInfo(Icon.Handle, IconInfo) then
begin
CPOffset := CursorInfo.ptScreenPos; // Get Point from GetCursorPos or MonitorFromPoint...
// This calculation need it, because the primary monitor are right and the secondary are left.
// Then has the secondary negative values. Sample:
// 0 ===== Bitmap is ===== 4480
// -2560 ========== 0 ========== 1920
// ========x============== mouse click -1460 on secondary monitor
// Abs(-2560) - Abs(1460) = 1100 from left point 0 at bitmap
//
// =================x===== mouse click 200 on primary monitor
// Abs(-2560) + 200 = 2760 from left point 0 at bitmap
if (Mon.Left < 0) then
begin
CPOffset.X := Abs(Mon.Left) - Abs(FMousePoint.X);
end
else
begin
if (FMousePoint.X > 0) then
begin
var SumWidth := 0;
for var Idx := 0 to Screen.MonitorCount - 1 do
begin
if (Screen.Monitors[Idx].Left < 0) then
begin
SumWidth := SumWidth + Abs(Screen.Monitors[Idx].Width);
end;
end;
CPOffset.X := SumWidth + FMousePoint.X;
end;
// CPOffset.Y := CPOffset.Y + M.Top;
end;
end;
// ABMP.Canvas.Draw((CPOffset.X - Integer(IconInfo.xHotspot) - CanvasClipRect.Left), (CPOffset.Y - Integer(IconInfo.yHotspot) - CanvasClipRect.Top), Icon);
// ABMP.Canvas.Draw((CursorInfo.ptScreenPos.X - IconInfo.xHotspot - CanvasClipRect.Left), (CursorInfo.ptScreenPos.Y - IconInfo.yHotspot - CanvasClipRect.Top), Icon);
// Sample to see it: ABMP.Canvas.Draw(2700, 200, Icon);
// ABMP.Canvas.Draw(CursorInfo.ptScreenPos.x - Integer(IconInfo.xHotspot) - R.Left, CursorInfo.ptScreenPos.y - Integer(IconInfo.yHotspot) - R.Top, Icon);
ABMP.Canvas.Draw(CPOffset.X, CPOffset.Y, Icon);
end;
end;
finally
Icon.Free;
end;
end;
function TClickRecorderScreenshotProvider.IsRecording: Boolean;
begin
Result := FIsRecording;
end;
function TClickRecorderScreenshotProvider.TakeScreenshot(var ABMP: Vcl.Graphics.TBitmap; AMonitorNumber: Integer): Boolean;
const
CAPTUREBLT = $40000000;
var
DesktopCanvas: TCanvas;
DC: HDC;
Left, Top: Integer;
begin
Result := False;
if (AMonitorNumber > Screen.MonitorCount) then
Exit;
DC := GetDC(0);
try
if (DC = 0) then
Exit;
if (AMonitorNumber = 0) then
begin
ABMP.Width := Screen.DesktopWidth;
ABMP.Height := Screen.DesktopHeight;
Left := Screen.DesktopLeft;
Top := Screen.DesktopTop;
end
else
begin
ABMP.Width := Screen.Monitors[AMonitorNumber - 1].Width;
ABMP.Height := Screen.Monitors[AMonitorNumber - 1].Height;
Left := Screen.Monitors[AMonitorNumber - 1].Left;
Top := Screen.Monitors[AMonitorNumber - 1].Top;
end;
DesktopCanvas := TCanvas.Create;
try
DesktopCanvas.Handle := DC;
Result := BitBlt(ABMP.Canvas.Handle, 0, 0, ABMP.Width, ABMP.Height, DesktopCanvas.Handle, Left, Top, SRCCOPY or CAPTUREBLT);
Result := True;
finally
DesktopCanvas.Free;
end;
finally
if (DC <> 0) then
ReleaseDC(0, DC);
end;
end;
function TClickRecorderScreenshotProvider.ScreenShot(ABMP: Vcl.Graphics.TBitmap; const ADrawCursor: Boolean; const AAMonitor: Integer): Boolean;
begin
Result := TakeScreenshot(ABMP, AAMonitor);
if Result then
begin
if FScreenShotDrawCursor then
begin
DrawScreenCursor(ABMP, AAMonitor);
end;
end;
end;
function TClickRecorderScreenshotProvider.TakeAScreenshot(APoint: TPoint): Vcl.Graphics.TBitmap;
begin
FMousePoint := APoint;
Result := Vcl.Graphics.TBitmap.Create;
ScreenShot(Result, FScreenShotDrawCursor, FScreenShotMonitor);
end;
end.