-
Notifications
You must be signed in to change notification settings - Fork 1
/
ClipboardUtils.pas
executable file
·188 lines (170 loc) · 4.18 KB
/
ClipboardUtils.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
unit ClipboardUtils;
interface
uses
Windows, Forms, Messages, SysUtils, Classes, clipbrd;
//function ClearClipboard: Boolean;
//procedure SaveClipboardToFile(FileName: TFileName);
//procedure LoadClipboardFromFile(FileName: TFileName);
implementation
type
TMemoryStreamEx = class(TMemoryStream)
public
procedure WriteInteger(Value: Integer);
function ReadInteger: Integer;
procedure WriteBoolean(Value: Boolean);
function ReadBoolean: Boolean;
procedure WriteString(Value: string);
procedure WriteStringEx(Value: array of Char);
function ReadString: string;
end;
procedure TMemoryStreamEx.WriteInteger(Value: Integer);
begin
Write(Value, SizeOf(Value));
end;
function TMemoryStreamEx.ReadInteger: Integer;
begin
Read(Result, SizeOf(Result));
end;
procedure TMemoryStreamEx.WriteBoolean(Value: Boolean);
begin
WriteInteger(Integer(Value));
end;
function TMemoryStreamEx.ReadBoolean: Boolean;
begin
Result := Boolean(ReadInteger);
end;
procedure TMemoryStreamEx.WriteString(Value: string);
var
StrLength: Integer;
begin
StrLength := Length(Value);
WriteInteger(StrLength);
WriteBuffer(Value[1], StrLength);
end;
procedure TMemoryStreamEx.WriteStringEx(Value: array of Char);
var
S: string;
begin
S := Value;
WriteString(S);
end;
function TMemoryStreamEx.ReadString: string;
var
StrLength: Integer;
begin
StrLength := ReadInteger;
SetLength(Result, StrLength);
ReadBuffer(Result[1], StrLength);
end;
function ClearClipboard: Boolean;
begin
OpenClipboard(Application.Handle);
try
Result := EmptyClipboard;
finally
CloseClipboard;
end;
end;
procedure SaveClipboardToStream(Stream: TMemoryStreamEx; Format: Integer);
var
Data: THandle;
DataPtr: Pointer;
DataSize: LongInt;
buff: array[0..127] of Char;
CustomFormat: Boolean;
begin
OpenClipboard(Application.Handle);
try
Data := GetClipboardData(Format);
if Data <> 0 then
begin
DataPtr := GlobalLock(Data);
if DataPtr <> nil then
begin
DataSize := GlobalSize(Data);
try
FillChar(buff, SizeOf(buff), #0);
CustomFormat := GetClipboardFormatName(Format, @buff, SizeOf(buff)) <> 0;
Stream.WriteBoolean(CustomFormat);
if CustomFormat then
Stream.WriteString(buff) else
Stream.WriteInteger(Format);
Stream.WriteInteger(DataSize);
Stream.WriteBuffer(DataPtr^, DataSize);
finally
GlobalUnlock(Data);
end;
end;
end;
finally
CloseClipboard;
end;
end;
procedure SaveClipboardToFile(FileName: TFileName);
var
I: Integer;
Stream: TMemoryStreamEx;
begin
Stream := TMemoryStreamEx.Create;
try
for I := 0 to Pred(Clipboard.FormatCount) do
SaveClipboardToStream(Stream, Clipboard.Formats[I]);
Stream.SaveToFile(FileName);
finally
Stream.Free;
end;
end;
procedure LoadClipboardFromStream(Stream: TMemoryStreamEx);
var
Data: THandle;
DataPtr: Pointer;
DataSize: LongInt;
Format: Integer;
FormatName: string;
CustomFormat: Boolean;
begin
OpenClipboard(Application.Handle);
try
CustomFormat := Stream.ReadBoolean;
if CustomFormat then
begin
FormatName := Stream.ReadString;
Format := RegisterClipboardFormat(PChar(FormatName));
end else
begin
Format := Stream.ReadInteger;
end;
DataSize := Stream.ReadInteger;
Data := GlobalAlloc(GMEM_MOVEABLE, DataSize);
try
DataPtr := GlobalLock(Data);
try
Stream.ReadBuffer(DataPtr^, DataSize);
SetClipboardData(Format, Data);
finally
GlobalUnlock(Data);
end;
except
GlobalFree(Data);
raise;
end;
finally
CloseClipboard;
end;
end;
procedure LoadClipboardFromFile(FileName: TFileName);
var
Stream: TMemoryStreamEx;
begin
Stream := TMemoryStreamEx.Create;
try
Stream.LoadFromFile(FileName);
Stream.Position := 0;
ClearClipboard;
while Stream.Position < Stream.Size do
LoadClipboardFromStream(Stream);
finally
Stream.Free;
end;
end;
end.