-
Notifications
You must be signed in to change notification settings - Fork 1
/
VisualClipboard.pas
executable file
·428 lines (396 loc) · 12.3 KB
/
VisualClipboard.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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
unit VisualClipboard;
interface
uses
Windows, Forms, Messages, SysUtils, Classes, Graphics, Controls, clipbrd;
type
TVisualClipboard = class(TGraphicControl)
private
FStream: TMemoryStream;
FDisplayString: WideString;
FDataTypeAsString: WideString;
FDisplayStringNeedsUpdate: Boolean;
FCaption: WideString;
procedure SetCaption(const Value: WideString);
protected
procedure Paint; override;
function DisplayString: WideString;
procedure UpdateDisplayString;
public
DumpFile: String;
constructor Create(AOwner: TComponent); override;
destructor Destroy;
procedure CopyClipboardFrom(AVisualClipboard: TVisualClipboard);
procedure SaveToFile(FileName: TFileName);
procedure LoadFromFile(FileName: TFileName);
procedure CopyFromClipboard;
procedure PasteToClipboard;
published
property Caption: WideString read FCaption write SetCaption;
property DragKind;
property DragCursor;
property DragMode;
property OnClick;
property OnDragDrop;
property OnDragOver;
property OnMouseDown;
property OnMouseUp;
property OnMouseMove;
end;
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
if GetOpenClipboardWindow() <> 0 then
Exit;
OpenClipboard(Application.Handle);
//ClipBrd.Clipboard.Open;
try
Result := EmptyClipboard;
finally
CloseClipboard;
// ClipBrd.Clipboard.Close;
end;
end;
procedure SaveClipboardToStream(Stream: TMemoryStreamEx; Format: Integer);
var
Data: THandle;
DataPtr: Pointer;
DataSize: LongInt;
buff: array[0..127] of Char;
CustomFormat: Boolean;
begin
if GetOpenClipboardWindow() <> 0 then
Exit;
OpenClipboard(Application.Handle);
//ClipBrd.Clipboard.Open;
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;
// ClipBrd.Clipboard.Close;
end;
end;
procedure LoadClipboardFromStream(Stream: TMemoryStreamEx);
var
Data: THandle;
DataPtr: Pointer;
DataSize: LongInt;
Format: Integer;
FormatName: string;
CustomFormat: Boolean;
begin
if GetOpenClipboardWindow() <> 0 then
Exit;
OpenClipboard(Application.Handle);
//ClipBrd.Clipboard.Open;
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;
// / ClipBrd.Clipboard.Close;
end;
end;
procedure DrawNiceFrame(ACanvas: TCanvas; var ARect: TRect; AFaceColor,
AFrameColor, ABackColor: TColor; AFrameSize: Integer);
var
I: Integer;
begin
ACanvas.Brush.Color := ABackColor;
ACanvas.FillRect(Rect(ARect.Left, ARect.Top, ARect.Right, ARect.Top + 15));
ACanvas.Brush.Color := AFrameColor;
ACanvas.Pen.Color := AFrameColor;
ACanvas.MoveTo(ARect.Left + 5, ARect.Top); ACanvas.LineTo(ARect.Right - 5, ARect.Top);
ACanvas.MoveTo(ARect.Left + 3, ARect.Top + 1); ACanvas.LineTo(ARect.Right - 3, ARect.Top + 1);
ACanvas.MoveTo(ARect.Left + 2, ARect.Top + 2); ACanvas.LineTo(ARect.Right - 2, ARect.Top + 2);
ACanvas.MoveTo(ARect.Left + 1, ARect.Top + 3); ACanvas.LineTo(ARect.Right - 1, ARect.Top + 3);
ACanvas.MoveTo(ARect.Left + 1, ARect.Top + 4); ACanvas.LineTo(ARect.Right - 1, ARect.Top + 4);
ACanvas.Rectangle(Rect(ARect.Left, ARect.Top + 5, ARect.Right, ARect.Top + 15));
ACanvas.Pen.Width := 1;
ACanvas.Brush.Color := AFaceColor;
ARect.Top := ARect.Top + 15;
if AFrameSize > 0 then
begin
ACanvas.Pen.Color := AFrameColor;
ACanvas.Rectangle(ARect);
InflateRect(ARect, -1, -1);
for I := 1 to AFrameSize do
begin
ACanvas.Brush.Color := AFrameColor;
ACanvas.FrameRect(ARect);
InflateRect(ARect, -1, -1);
end;
end;
end;
{ TVisualClipboard }
procedure TVisualClipboard.CopyClipboardFrom(AVisualClipboard: TVisualClipboard);
begin
if Assigned(AVisualClipboard) then
begin
FreeAndNil(FStream);
FStream := TMemoryStreamEx.Create;
AVisualClipboard.FStream.Position := 0;
FStream.Size := AVisualClipboard.FStream.Size;
FStream.CopyFrom(AVisualClipboard.FStream, AVisualClipboard.FStream.Size);
FDisplayStringNeedsUpdate := True;
Invalidate;
Update;
end;
end;
procedure TVisualClipboard.CopyFromClipboard;
var
I: Integer;
begin
FreeAndNil(FStream);
FStream := TMemoryStreamEx.Create;
for I := 0 to Pred(Clipboard.FormatCount) do
SaveClipboardToStream(TMemoryStreamEx(FStream), Clipboard.Formats[I]);
FDisplayStringNeedsUpdate := True;
end;
constructor TVisualClipboard.Create(AOwner: TComponent);
begin
inherited;
FStream := TMemoryStreamEx.Create;
FDisplayString := '';
FDisplayStringNeedsUpdate := False;
end;
destructor TVisualClipboard.Destroy;
begin
if Assigned(FStream) then
FreeAndNil(FStream);
end;
function TVisualClipboard.DisplayString: WideString;
begin
if FDisplayStringNeedsUpdate then
UpdateDisplayString;
FDisplayStringNeedsUpdate := False;
Result := FDisplayString;
end;
procedure TVisualClipboard.LoadFromFile(FileName: TFileName);
begin
FStream.LoadFromFile(FileName);
FDisplayStringNeedsUpdate := True;
end;
procedure TVisualClipboard.Paint;
var
r1: TRect;
w: WideString;
begin
inherited;
r1 := ClientRect;
DrawNiceFrame(Canvas, r1, clWindow, 14914662, clWindow, 3);
InflateRect(r1, -3, -3);
w := DisplayString;
Canvas.Brush.Color := clWindow;
Canvas.Font.Color := clDkGray;
DrawTextExW(Canvas.Handle, PWideChar(w), Length(w), r1, DT_END_ELLIPSIS, nil);
r1 := Rect(12, 2, ClientWidth - 12, 15);
Canvas.Brush.Color := 14914662;
Canvas.Font.Color := clWhite;
w := FCaption + ' ' + FDataTypeAsString;
DrawTextExW(Canvas.Handle, PWideChar(w), Length(w), r1, DT_END_ELLIPSIS, nil);
end;
procedure TVisualClipboard.PasteToClipboard;
begin
ClearClipboard;
FStream.Position := 0;
while FStream.Position < FStream.Size do
LoadClipboardFromStream(TMemoryStreamEx(FStream));
end;
procedure TVisualClipboard.SaveToFile(FileName: TFileName);
begin
FStream.SaveToFile(FileName);
end;
procedure TVisualClipboard.SetCaption(const Value: WideString);
begin
if FCaption <> Value then
begin
FCaption := Value;
Invalidate;
end;
end;
procedure TVisualClipboard.UpdateDisplayString;
procedure ProcessClipboardFormat(Stream: TMemoryStreamEx);
var
DataSize: LongInt;
Format: Integer;
FormatName: string;
CustomFormat: Boolean;
AAnsiString: AnsiString;
begin
CustomFormat := Stream.ReadBoolean;
if CustomFormat then
begin
FormatName := Stream.ReadString;
FDisplayString := FDisplayString + FormatName + #13#10;
DataSize := Stream.ReadInteger;
Stream.Position := FStream.Position + DataSize;
end
else
begin
Format := Stream.ReadInteger;
DataSize := Stream.ReadInteger;
case Format of
{CF_TEXT,} CF_OEMTEXT: begin
if DataSize > 256 then
begin
SetLength(AAnsiString, 256);
Stream.ReadBuffer((@AAnsiString[1])^, 256);
FDisplayString := FDisplayString + AAnsiString + #13#10;
DataSize := DataSize - 256;
end
else
begin
SetLength(AAnsiString, DataSize);
Stream.ReadBuffer((@AAnsiString[1])^, DataSize);
FDisplayString := FDisplayString + AAnsiString + #13#10;
DataSize := 0;
end;
if FDataTypeAsString <> 'Bitmap' then
FDataTypeAsString := 'Text';
end;
CF_BITMAP,CF_DIB: FDataTypeAsString := 'Bitmap';
//CF_DIB: FDataTypeAsString := 'Bitmap';
{CF_BITMAP: FDisplayString := FDisplayString + 'CF_BITMAP' + #13#10;
CF_METAFILEPICT: FDisplayString := FDisplayString + 'CF_METAFILEPICT' + #13#10;
CF_SYLK: FDisplayString := FDisplayString + 'CF_SYLK' + #13#10;
CF_DIF: FDisplayString := FDisplayString + 'CF_DIF' + #13#10;
CF_TIFF: FDisplayString := FDisplayString + 'CF_TIFF' + #13#10;
CF_OEMTEXT: begin
end;
CF_DIB: FDisplayString := FDisplayString + 'CF_DIB' + #13#10;
CF_PALETTE: FDisplayString := FDisplayString + 'CF_PALETTE' + #13#10;
CF_PENDATA: FDisplayString := FDisplayString + 'CF_PENDATA' + #13#10;
CF_RIFF: FDisplayString := FDisplayString + 'CF_RIFF' + #13#10;
CF_WAVE: FDisplayString := FDisplayString + 'CF_WAVE' + #13#10;
CF_UNICODETEXT: begin
if DataSize > 512 then
begin
SetLength(AWideString, 256);
Stream.ReadBuffer((@AAnsiString[1])^, 2*256);
FDisplayString := FDisplayString + AWideString + #13#10;
DataSize := DataSize - 2*256;
end
else
begin
SetLength(AWideString, DataSize);
Stream.ReadBuffer((@AWideString[1])^, DataSize);
FDisplayString := FDisplayString + AWideString + #13#10;
DataSize := 0;
end;
end;
CF_ENHMETAFILE: FDisplayString := FDisplayString + 'CF_ENHMETAFILE' + #13#10;
CF_HDROP: FDisplayString := FDisplayString + 'CF_HDROP' + #13#10;
CF_LOCALE: FDisplayString := FDisplayString + 'CF_LOCALE' + #13#10;
CF_MAX: FDisplayString := FDisplayString + 'CF_MAX' + #13#10; }
else
// FDisplayString := FDisplayString + 'CF_UNKNOWN' + #13#10;
end;
FStream.Position := FStream.Position + DataSize;
end;
end;
begin
FDisplayString := '';
FStream.Position := 0;
FDataTypeAsString := '';
while FStream.Position < FStream.Size do
ProcessClipboardFormat(TMemoryStreamEx(FStream));
if (Pos('FileName', FDisplayString) > 0)and(Pos('FileNameW', FDisplayString) > 0)and(Pos('Ole Private Data', FDisplayString) > 0) then
begin
FDisplayString := '[File]';
FDataTypeAsString := 'Explorer';
end;
Hint := FDisplayString;
ShowHint := Length(FDisplayString) > 0;
end;
end.