Difference between revisions of "Clipboard"

From Free Pascal wiki
Jump to navigationJump to search
m (Text replace - "delphi>" to "syntaxhighlight>")
Line 46: Line 46:
  
 
Writing text:
 
Writing text:
<delphi>Clipboard.AsText := 'Hello clipboard!';</delphi>
+
<syntaxhighlight>Clipboard.AsText := 'Hello clipboard!';</syntaxhighlight>
  
 
Reading text:
 
Reading text:
<delphi>ShowMessage('Clipboard content: ' + Clipboard.AsText);</delphi>
+
<syntaxhighlight>ShowMessage('Clipboard content: ' + Clipboard.AsText);</syntaxhighlight>
  
 
==Text oriented components==
 
==Text oriented components==
Line 55: Line 55:
 
Some visual components like TEdit, TMemo, TStringGrid, TLabeledEdit, TMaskEdit, TSpinEdit and TFloatSpinEdit have ability to select a part of contained text and offer additional functionality for handling clipboard operations on selected text.  
 
Some visual components like TEdit, TMemo, TStringGrid, TLabeledEdit, TMaskEdit, TSpinEdit and TFloatSpinEdit have ability to select a part of contained text and offer additional functionality for handling clipboard operations on selected text.  
  
<delphi>  procedure CopyToClipboard;  
+
<syntaxhighlight>  procedure CopyToClipboard;  
 
   procedure CutToClipboard;  
 
   procedure CutToClipboard;  
   procedure PasteFromClipboard;</delphi>
+
   procedure PasteFromClipboard;</syntaxhighlight>
  
 
==HTML source==
 
==HTML source==
  
 
Example of reading HTML source from clipboard:
 
Example of reading HTML source from clipboard:
<delphi>uses
+
<syntaxhighlight>uses
 
   Clipbrd, ...;
 
   Clipbrd, ...;
  
Line 85: Line 85:
 
     Stream.Free;
 
     Stream.Free;
 
   end;
 
   end;
end;</delphi>
+
end;</syntaxhighlight>
  
 
Write html source to clipboard. This can be done with AddFormat either using TStream or memory Buffer.
 
Write html source to clipboard. This can be done with AddFormat either using TStream or memory Buffer.
<delphi>// register the mime type for text/html. You can do this once at program start:
+
<syntaxhighlight>// register the mime type for text/html. You can do this once at program start:
 
   ClipbrdFmtHTML := RegisterClipboardFormat('text/html');
 
   ClipbrdFmtHTML := RegisterClipboardFormat('text/html');
 
...
 
...
Line 95: Line 95:
 
   Clipboard.AsText := ThePlainUTF8Text;  
 
   Clipboard.AsText := ThePlainUTF8Text;  
 
   HTMLSource := '<b>Formatted</b> text'; // text with formatrings
 
   HTMLSource := '<b>Formatted</b> text'; // text with formatrings
   Clipboard.AddFormat(ClipbrdFmtHTML, HTMLSource[1], Length(HTMLSource));</delphi>
+
   Clipboard.AddFormat(ClipbrdFmtHTML, HTMLSource[1], Length(HTMLSource));</syntaxhighlight>
  
 
==Image==
 
==Image==
Line 101: Line 101:
 
===Load from clipboard===
 
===Load from clipboard===
  
<delphi>uses  
+
<syntaxhighlight>uses  
 
   Clipbrd, LCLIntf, LCLType, ...;
 
   Clipbrd, LCLIntf, LCLType, ...;
  
Line 110: Line 110:
 
   if Clipboard.HasFormat(PredefinedClipboardFormat(pcfBitmap)) then
 
   if Clipboard.HasFormat(PredefinedClipboardFormat(pcfBitmap)) then
 
     Bitmap.LoadFromClipboardFormat(PredefinedClipboardFormat(pcfBitmap));
 
     Bitmap.LoadFromClipboardFormat(PredefinedClipboardFormat(pcfBitmap));
end;</delphi>
+
end;</syntaxhighlight>
  
 
===Save to clipboard===
 
===Save to clipboard===
  
<delphi>uses  
+
<syntaxhighlight>uses  
 
   Clipbrd, ...;
 
   Clipbrd, ...;
  
Line 120: Line 120:
 
begin
 
begin
 
   Clipboard.Assign(Bitmap);
 
   Clipboard.Assign(Bitmap);
end;</delphi>
+
end;</syntaxhighlight>
  
 
==Custom format==
 
==Custom format==
Line 132: Line 132:
 
Sample code to implement message handler:
 
Sample code to implement message handler:
  
<delphi>unit Unit1;
+
<syntaxhighlight>unit Unit1;
  
 
{$mode delphi}{$H+}
 
{$mode delphi}{$H+}
Line 211: Line 211:
 
end;
 
end;
  
end.</delphi>
+
end.</syntaxhighlight>
  
 
==External links==
 
==External links==
  
 
* [http://delphi.about.com/od/vclusing/a/tclipboard.htm Basic Clipboard Operations (Cut/Copy/Paste) using the TClipboard object]
 
* [http://delphi.about.com/od/vclusing/a/tclipboard.htm Basic Clipboard Operations (Cut/Copy/Paste) using the TClipboard object]

Revision as of 14:58, 24 March 2012

Predefined types

TPredefinedClipboardFormat MIME type
pcfText text/plain
pcfBitmap image/bmp
pcfPixmap image/xpm
pcfIcon image/lcl.icon
pcfPicture image/lcl.picture
pcfObject application/lcl.object
pcfComponent application/lcl.component
pcfCustomData application/lcl.customdata
pcfDelphiText text/plain
pcfDelphiBitmap text/delphi.bitmap
pcfDelphiPicture Delphi picture
pcfDelphiMetaFilePict image/delphimetafilepict
pcfDelphiObject application/delphi.object
pcfDelphiComponent Delphi component
pcfKylixPicture image/delphi.picture
pcfKylixBitmap image/delphi.bitmap
pcfKylixDrawing image/delphi.drawing
pcfKylixComponent application/delphi.component

Text

For use of simple text Clipboard offer property AsText which can be used for reading an writing plain text.

Writing text:

Clipboard.AsText := 'Hello clipboard!';

Reading text:

ShowMessage('Clipboard content: ' + Clipboard.AsText);

Text oriented components

Some visual components like TEdit, TMemo, TStringGrid, TLabeledEdit, TMaskEdit, TSpinEdit and TFloatSpinEdit have ability to select a part of contained text and offer additional functionality for handling clipboard operations on selected text.

  procedure CopyToClipboard; 
  procedure CutToClipboard; 
  procedure PasteFromClipboard;

HTML source

Example of reading HTML source from clipboard:

uses
  Clipbrd, ...;

procedure InsertHTMLSourceFromClipboard(Strings: TString);
var
  Fid: TClipboardFormat
  Str: WideString;
  Stream: TMemoryStream;
begin
  Fid := Clipboard.FindFormatID('text/html');
  if Fid <> 0 then 
  try
    Stream := TMemoryStream.Create;
    if Clipboard.GetFormat(Fid, Stream) then 
    begin
      Stream.Position := 0;
      Str := PWideChar(Stream.Memory);
      Str[(Stream.Size div 2) + 1] := #0;
      Strings.Text := UTF8Encode(Str);
    end;
  finally
    Stream.Free;
  end;
end;

Write html source to clipboard. This can be done with AddFormat either using TStream or memory Buffer.

// register the mime type for text/html. You can do this once at program start:
  ClipbrdFmtHTML := RegisterClipboardFormat('text/html');
...
  // put text and html on the clipboard. Other applications will choose the best format automatically.
  ThePlainUTF8Text := 'Simple text';
  Clipboard.AsText := ThePlainUTF8Text; 
  HTMLSource := '<b>Formatted</b> text'; // text with formatrings
  Clipboard.AddFormat(ClipbrdFmtHTML, HTMLSource[1], Length(HTMLSource));

Image

Load from clipboard

uses 
  Clipbrd, LCLIntf, LCLType, ...;

procedure LoadBitmapFromClipboard(Bitmap: TBitmap);
begin
  if Clipboard.HasFormat(PredefinedClipboardFormat(pcfDelphiBitmap)) then
    Bitmap.LoadFromClipboardFormat(PredefinedClipboardFormat(pcfDelphiBitmap));
  if Clipboard.HasFormat(PredefinedClipboardFormat(pcfBitmap)) then
    Bitmap.LoadFromClipboardFormat(PredefinedClipboardFormat(pcfBitmap));
end;

Save to clipboard

uses 
  Clipbrd, ...;

procedure SaveBitmapToClipboard(Bitmap: TBitmap);
begin
  Clipboard.Assign(Bitmap);
end;

Custom format

Multiple objects

Getting notified of changes

The LCL does not pass on Windows messages. It only passes on messages > WM_USER. This means you have to write your own message handler. Processing non - user messages in your window

Sample code to implement message handler:

unit Unit1;

{$mode delphi}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,
  Clipbrd, StdCtrls, Windows, Messages;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    FNextClipboardOwner: HWnd;   // handle to the next viewer
    // Here are the clipboard event handlers
    function WMChangeCBChain(wParam: WParam; lParam: LParam):LRESULT;
    function WMDrawClipboard(wParam: WParam; lParam: LParam):LRESULT;
  public
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}
var
  PrevWndProc:windows.WNDPROC;

function WndCallback(Ahwnd: HWND; uMsg: UINT; wParam: WParam;
  lParam: LParam): LRESULT; stdcall;
begin
  if uMsg = WM_CHANGECBCHAIN then begin
    Result := Form1.WMChangeCBChain(wParam, lParam);
    Exit;
  end 
  else if uMsg=WM_DRAWCLIPBOARD then begin
    Result := Form1.WMDrawClipboard(wParam, lParam);
    Exit;
  end;
  Result := CallWindowProc(PrevWndProc, Ahwnd, uMsg, WParam, LParam);
end;

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
begin
  PrevWndProc := Windows.WNDPROC(SetWindowLong(Self.Handle, GWL_WNDPROC, PtrInt(@WndCallback)));
  FNextClipboardOwner := SetClipboardViewer(Self.Handle);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  ChangeClipboardChain(Handle, FNextClipboardOwner);
end;

function TForm1.WMChangeCBChain(wParam: WParam; lParam: LParam): LRESULT;
var
  Remove, Next: THandle;
begin
  Remove := WParam;
  Next := LParam;
  if FNextClipboardOwner = Remove then FNextClipboardOwner := Next
    else if FNextClipboardOwner <> 0 then
      SendMessage(FNextClipboardOwner, WM_ChangeCBChain, Remove, Next)
end;

function TForm1.WMDrawClipboard(wParam: WParam; lParam: LParam): LRESULT;
begin
  if Clipboard.HasFormat(CF_TEXT) Then Begin
    ShowMessage(Clipboard.AsText);
  end;
  SendMessage(FNextClipboardOwner, WM_DRAWCLIPBOARD, 0, 0);   // VERY IMPORTANT
  Result := 0;
end;

end.

External links