Eastar
asked on
How to backup the clipboard?
I'm writing an app for win95/NT. One of the routines requires me to save the windows' clipboard and restore the original clipboard data later.
Is it possible to save the entire clipboard temporily?
Some sample code is great appreciating.
Thanks.
Is it possible to save the entire clipboard temporily?
Some sample code is great appreciating.
Thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
(Edited by Computer101) you (Edited by Computer101)!!!!!!!!!
I agree... umitde's answer was lame and doesn't do a good job at all. Be careful to give the points to the best answer if you want people to help you.
Here is an example that saves all clipboard data and restores it again, no matter what it is. Text, images, Icons... whatever.
Button1 saves all clipboard data to this applications memory. Button2 restores the data back to the clipboard and frees up this applications memory again.
Regards,
Epsylon.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Clipbrd;
type
PClips = ^TClips;
TClips = record
cf: Word;
buf: Pointer;
size: Cardinal;
end;
TForm1 = class(TForm)
Button1: TButton;
ListBox1: TListBox;
ListBox2: TListBox;
ListBox3: TListBox;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
ClipList: TList;
procedure GetClippedData(cf: Cardinal; var buf: Pointer; var size: Cardinal);
function SetClippedData(cf: Cardinal; buf: Pointer; size: Cardinal): boolean;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender
var i: Integer;
blk: PClips;
begin
ClipList := TList.Create;
for i := 0 to Clipboard.FormatCount - 1 do
begin
New(blk);
blk.cf := Clipboard.Formats[i];
GetClippedData(blk.cf, blk.buf, blk.size);
ClipList.Add(blk);
ListBox1.Items.Add(IntToSt
ListBox2.Items.Add(IntToSt
ListBox3.Items.Add(IntToSt
end;
end;
procedure TForm1.Button2Click(Sender
var blk: PClips;
begin
Clipboard.Clear;
while ClipList.Count > 0 do
begin
blk := ClipList.Items[0];
SetClippedData(blk.cf, blk.buf, blk.size);
Dispose(blk);
ClipList.Delete(0);
end;
ClipList.Free;
end;
procedure TForm1.GetClippedData(cf: Cardinal; var buf: Pointer; var size: Cardinal);
var hmem: Cardinal;
lock: Pointer;
begin
if OpenClipboard(Handle) then
begin
hmem := GetClipboardData(cf);
if hmem = 0 then
buf := nil
else begin
size := GlobalSize(hmem);
buf := AllocMem(size);
lock := GlobalLock(hmem);
CopyMemory(buf, lock, size);
GlobalUnlock(hmem);
end;
CloseClipboard;
end
else
buf := nil;
end;
function TForm1.SetClippedData(cf: Cardinal; buf: Pointer; size: Cardinal): boolean;
var hmem, sd: Cardinal;
lock: Pointer;
begin
// Allocate memory in the global heap
// Do not free it in this app. It will be freed when the clipboard is cleared
hmem := GlobalAlloc(GMEM_DDESHARE or GMEM_MOVEABLE, size);
lock := GlobalLock(hmem);
CopyMemory(lock, buf, size);
FreeMem(buf);
GlobalUnlock(hmem);
if OpenClipboard(Handle) then
begin
sd := SetClipboardData(cf, hmem);
CloseClipboard;
Result := (sd <> 0);
end
else
Result := false;
end;
end.