Clipboard

Show clipboard's content

Posted in Clipboard

This example shows how to view clipboard contents as text or bitmap:

uses Clipboard;
...
procedure TForm1.Button1Click(Sender: TObject);
var
  MyHandle: THandle;
begin
  Clipboard.Open;
  if Clipboard.HasFormat(CF_TEXT) then
  begin
    MyHandle:=Clipboard.GetAsHandle(CF_TEXT);
    Memo1.Lines.Add(StrPas(GlobalLock(MyHandle)));
    GlobalUnlock(MyHandle);
  end;
  if (Clipboard.HasFormat(CF_BITMAP)) or
    (Clipboard.HasFormat(CF_PICTURE)) then
    Image1.Picture.Assign(Clipboard);
  Clipboard.Close;
end;

Paste/copy memo's contents

Posted in Clipboard

To paste something from the clipboard, use PasteFromClipboard method of control. To copy something to the clipboard use CopyToClipboard.

procedure TForm1.Button2Click(Sender: TObject);
begin
  Memo1.SelectAll;
  Memo1.CopyToClipboard;
  Memo1.Clear;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Memo2.PasteFromClipboard;
end;

Show clipboard's content

Posted in Clipboard

This example shows how to view clipboard contents as text or bitmap:

uses Clipboard;
...
procedure TForm1.Button1Click(Sender: TObject);
var
  MyHandle: THandle;
begin
  Clipboard.Open;
  if Clipboard.HasFormat(CF_TEXT) then
  begin
    MyHandle:=Clipboard.GetAsHandle(CF_TEXT);
    Memo1.Lines.Add(StrPas(GlobalLock(MyHandle)));
    GlobalUnlock(MyHandle);
  end;
  if (Clipboard.HasFormat(CF_BITMAP)) or
    (Clipboard.HasFormat(CF_PICTURE)) then
    Image1.Picture.Assign(Clipboard);
  Clipboard.Close;
end;

Paste/copy memo's contents

Posted in Clipboard

To paste something from the clipboard, use PasteFromClipboard method of control. To copy something to the clipboard use CopyToClipboard.

procedure TForm1.Button2Click(Sender: TObject);
begin
  Memo1.SelectAll;
  Memo1.CopyToClipboard;
  Memo1.Clear;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Memo2.PasteFromClipboard;
end;

Monitor clipboard's content at runtime

Posted in Clipboard

Use DisplayClipboard procedure for monitoring the clipboard content of text or bitmap formats at runtime.

...
  private
    procedure DisplayClipboard(var Msg: TWMDrawClipBoard); 
      message WM_DRAWCLIPBOARD;
...
var
  Form1: TForm1;
  ClipboardWindow: HWND;

implementation

{$R *.DFM}

procedure TForm1.DisplayClipboard(var Msg: TWMDrawClipBoard);
var
  MyHandle: THandle;
begin
  if IsClipboardFormatAvailable(CF_TEXT) or
     IsClipboardFormatAvailable(CF_BITMAP) then
  begin
    OpenClipboard(Form1.Handle);
    if IsClipboardFormatAvailable(CF_TEXT)then
    begin
      MyHandle:=Clipboard.GetAsHandle(CF_TEXT);
      Memo1.Lines.Add(StrPas(GlobalLock(MyHandle)));
      GlobalUnlock(MyHandle);
    end;

    if IsClipboardFormatAvailable(CF_BITMAP) then
    begin
      Panel1.Visible:=True;
      Image1.Picture.Assign(Clipboard);
    end;
    CloseClipboard;
  end;
  SendMessage(ClipboardWindow, WM_DRAWCLIPBOARD, 0, 0);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  OpenClipboard(Form1.Handle);
  EmptyClipboard;
  CloseClipboard;
  Panel1.Visible:=False;
  ClipboardWindow:=SetClipboardViewer(Form1.Handle);
end;