Link to home
Start Free TrialLog in
Avatar of yingkit
yingkit

asked on

Clipboard backup

Hi there,
How can I make a copy of the content of the clipboard in the memory?  And how to restore the clipboard from the copy?

Thanks in advance.

PS. The content of the clipboard can be anything, not necessarily text
ASKER CERTIFIED SOLUTION
Avatar of Epsylon
Epsylon

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of edey
edey

Some of this stuff can be simplified using the TClipboard object (don't instantiate it, just add clipbrd to your uses clause).  The help files have these example for getting & setting data from/to the clipboard (also most vcl's have a methods to do this)

var

  MyHandle: THandle;
  TextPtr: PChar;
  MyString: string;
begin
  ClipBoard.Open;
try
  MyHandle := Clipboard.GetAsHandle(CF_TEXT);
  TextPtr := GlobalLock(MyHandle);
  MyString := StrPas(TextPtr);
  GlobalUnlock(MyHandle);
finally
  Clipboard.Close;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  MyFormat : Word;
  Bitmap : TBitMap;
  AData,APalette : THandle;
begin
  Bitmap := TBitmap.Create;
  try
    Bitmap.LoadFromFile('c:\Program Files\Common Files\Borland Shared\Images\Splash\256color\factory.bmp');
    Bitmap.SaveToClipBoardFormat(MyFormat,AData,APalette);
    ClipBoard.SetAsHandle(MyFormat,AData);
  finally
    Bitmap.Free;
  end;
end;

If, OTOH, you've got this stuff down & just need to know when to get the data (as there are no events for TClipboard) you would then need to register a clipBoardViewer:


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, clipbrd;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
    nextViewer : HWND;
    procedure doWMDRAWCLIPBOARD(var msg : TWMDRAWCLIPBOARD);message WM_DRAWCLIPBOARD;
    procedure doWMCHANGECBCHAIN(var msg : TWMCHANGECBCHAIN);message WM_CHANGECBCHAIN;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
     nextViewer := setClipBoardViewer(handle);
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
     changeClipBoardChain(handle,nextViewer);
end;

procedure TForm1.doWMCHANGECBCHAIN(var msg : TWMCHANGECBCHAIN);
begin
     if msg.remove = nextViewer then
        nextViewer := msg.Next
     else
         sendMessage(nextViewer,WM_CHANGECBCHAIN,msg.remove,msg.next);
end;

procedure TForm1.doWMDRAWCLIPBOARD(var msg : TWMDRAWCLIPBOARD);
begin
//this proc will get called anytime anything get's added
//to the clipboard.  From here you can query the cb as to
//current formats & data & such, as seen above
end;

end.


GL
Mike
Avatar of yingkit

ASKER

Thank you all.