-
Notifications
You must be signed in to change notification settings - Fork 33
/
HGM.Screenshot.pas
165 lines (145 loc) · 3.56 KB
/
HGM.Screenshot.pas
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
unit HGM.Screenshot;
interface
uses
System.Classes, System.SysUtils, FMX.Graphics, FMX.Types, {$IFDEF MSWINDOWS}Winapi.Windows, {$ENDIF MSWINDOWS}
Vcl.Forms, Vcl.Graphics;
type
TOnReadyFrame = procedure(Sender: TObject; Stream: TStream) of object;
TScreenRecorder = class
protected
procedure Execute;
private
FThread: TThread;
FActive: Boolean;
FStop: Boolean;
FOnReadyFrame: TOnReadyFrame;
FStream: TMemoryStream;
FInterval: Cardinal;
procedure SetActive(const Value: Boolean);
procedure SetOnReadyFrame(const Value: TOnReadyFrame);
procedure DoOnReadyFrame;
procedure SetInterval(const Value: Cardinal);
public
procedure Start;
procedure Stop(Wait: Boolean);
constructor Create;
destructor Destroy; override;
property Active: Boolean read FActive write SetActive;
property Interval: Cardinal read FInterval write SetInterval;
property OnReadyFrame: TOnReadyFrame read FOnReadyFrame write SetOnReadyFrame;
end;
procedure TakeScreenshot(Stream: TStream);
implementation
{$IFDEF MSWINDOWS}
procedure WriteWindowsToStream(Stream: TStream);
var
DC: HDC;
Palette: TLogPalette;
Bitmap: Vcl.Graphics.TBitmap;
begin
Bitmap := Vcl.Graphics.TBitmap.Create;
try
Bitmap.SetSize(Screen.Width, Screen.Height);
DC := GetDc(0);
if DC <> 0 then
begin
try
if (GetDeviceCaps(DC, RASTERCAPS) and RC_PALETTE = RC_PALETTE) then
begin
Palette.palVersion := $300;
Palette.palNumEntries := GetSystemPaletteEntries(DC, 0, 256, Palette.palPalEntry);
if Palette.PalNumEntries <> 0 then
Bitmap.Palette := CreatePalette(Palette);
end;
BitBlt(Bitmap.Canvas.Handle, 0, 0, Screen.Width, Screen.Height, DC, 0, 0, SRCCOPY);
Bitmap.SaveToStream(Stream);
finally
ReleaseDc(0, DC);
end;
end;
finally
Bitmap.Free;
end;
end;
{$ENDIF MSWINDOWS}
procedure TakeScreenshot(Stream: TStream);
begin
{$IFDEF MSWINDOWS}
WriteWindowsToStream(Stream);
{$ENDIF MSWINDOWS}
end;
{ TScreenRecorder }
constructor TScreenRecorder.Create;
begin
inherited;
FInterval := Round(1000 / 25); {25 êàäðîâ â ñåê}
FStream := TMemoryStream.Create;
end;
destructor TScreenRecorder.Destroy;
begin
Stop(True);
FStream.Free;
inherited;
end;
procedure TScreenRecorder.DoOnReadyFrame;
begin
if Assigned(FOnReadyFrame) then
FOnReadyFrame(Self, FStream);
end;
procedure TScreenRecorder.Execute;
begin
FActive := True;
try
while not FStop do
begin
FStream.Clear;
TakeScreenshot(FStream);
FStream.Position := 0;
if Assigned(FOnReadyFrame) then
TThread.Synchronize(nil, DoOnReadyFrame);
Sleep(FInterval);
end;
finally
FActive := False;
FStop := False;
end;
end;
procedure TScreenRecorder.SetActive(const Value: Boolean);
begin
if Value then
Start
else
FStop := True;
end;
procedure TScreenRecorder.SetInterval(const Value: Cardinal);
begin
FInterval := Value;
end;
procedure TScreenRecorder.SetOnReadyFrame(const Value: TOnReadyFrame);
begin
FOnReadyFrame := Value;
end;
procedure TScreenRecorder.Start;
begin
Stop(True);
FStop := False;
FThread := TThread.CreateAnonymousThread(Execute);
FThread.FreeOnTerminate := False;
FThread.Start;
end;
procedure TScreenRecorder.Stop(Wait: Boolean);
begin
FStop := True;
if Wait and Assigned(FThread) then
while not FThread.Finished do
begin
FThread.Yield;
Application.ProcessMessages;
end;
if Assigned(FThread) then
begin
FThread.Free;
FThread := nil;
end;
end;
end.