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.

EastarAsked:
Who is Participating?
 
umitdeConnect With a Mentor Commented:
 private
      temp : String;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
if Clipboard.HasFormat(CF_TEXT) then
  Temp := Clipboard.AsText
else
  MessageDlg('There is no text on the Clipboard', mtInformation,
        [mbOK],0);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Clipboard.AsText := Temp;
if Clipboard.HasFormat(CF_TEXT) then
  Edit1.Text := temp
else
  MessageDlg('There is no text on the Clipboard', mtInformation,
        [mbOK],0);
end;
You can take a look to TClipboard for Detail.
0
 
EpsylonCommented:
Eastar, Umitde's answer only saves text and not other data.

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: TObject);
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(IntToStr(blk.cf));
    ListBox2.Items.Add(IntToStr(Integer(blk.buf)));
    ListBox3.Items.Add(IntToStr(blk.size));
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
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.
0
 
EpsylonCommented:
(Edited by Computer101) you (Edited by Computer101)!!!!!!!!!
0
 
delphiexpertCommented:
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.
0
All Courses

From novice to tech pro — start learning today.