Basic Clipboard Operations (Cut/Copy/Paste) in Delphi

Using the TClipboard object

Programming Clipboard in Delphi

 CC0 Public Domain

http://pxhere.com/en/photo/860609

The Windows Clipboard represents the container for any text or graphics that are cut, copied or pasted from or to an application. This article will show you how to use the TClipboard object to implement cut-copy-paste features in your Delphi application.

Clipboard in General

As you probably know, the Clipboard can hold only one piece of the same kind of data for cut, copy and paste at one time. If we send new information in the same format to the Clipboard, we wipe out what was there before, but the contents of the Clipboard stays with the Clipboard even after we paste those contents into another program.

TClipboard

In order to use the Windows Clipboard in our applications, we must add the ClipBrd unit to the uses clause of the project, except when we restrict cutting, copying and pasting to the components already possessing built-in support for Clipboard methods. Those components are TEdit, TMemo, TOLEContainer, TDDEServerItem, TDBEdit, TDBImage and TDBMemo.

The ClipBrd unit automatically represents a TClipboard object called Clipboard. We'll use the CutToClipboard, CopyToClipboard, PasteFromClipboard, Clear and HasFormat methods to deal with Clipboard operations and text/graphic manipulation.

Send and Retrieve Text

In order to send some text to the Clipboard the AsText property of the Clipboard object is used. If we want, for example, to send the string information contained in the variable SomeStringData to the Clipboard (wiping out whatever text was there), we'll use the following code:

 uses ClipBrd;
...
Clipboard.AsText := SomeStringData_Variable; 

To retrieve the text information from the Clipboard we'll use

 uses ClipBrd;
...
SomeStringData_Variable := Clipboard.AsText; 

Note: if we only want to copy the text from, let's say, Edit component to the Clipboard, we do not have to include the ClipBrd unit to the uses clause. The CopyToClipboard method of TEdit copies the selected text in the edit control to the Clipboard in the CF_TEXT format.

 procedure TForm1.Button2Click(Sender: TObject) ;
begin
   //the following line will select    //ALL the text in the edit control    {Edit1.SelectAll;}
   Edit1.CopyToClipboard;
end; 

Clipboard Images

To retrieve graphical images from the Clipboard, Delphi must know what type of image is stored there. Similarly, to transfer images to the clipboard, the application must tell the Clipboard what type of graphics it is sending. Some of the possible values of the Format parameter follow; there are many more Clipboard formats provided by Windows.

  • CF_TEXT - Text with each line ending with a CR-LF combination.
  • CF_BITMAP - A Windows bitmap graphic.
  • CF_METAFILEPICT - A Windows metafile graphic.
  • CF_PICTURE - An object of type TPicture.
  • CF_OBJECT - Any persistent object.

The HasFormat method returns True if the image in the Clipboard has the right format:

 if Clipboard.HasFormat(CF_METAFILEPICT) then ShowMessage('Clipboard has metafile') ; 

Use the Assign method to send (assign) an image to the Clipboard. For example, the following code copies the bitmap from a bitmap object named MyBitmap to the Clipboard:

 Clipboard.Assign(MyBitmap) ; 

In general, MyBitmap is an object of type TGraphics, TBitmap, TMetafile or TPicture.

To retrieve an image from the Clipboard we have to: verify the format of the current contents of the clipboard and use the Assign method of the target object:

 {place one button and one image control on form1} {Prior to executing this code press Alt-PrintScreen key combination}
uses clipbrd;
...
procedure TForm1.Button1Click(Sender: TObject) ;
begin
if Clipboard.HasFormat(CF_BITMAP) then Image1.Picture.Bitmap.Assign(Clipboard) ;
end; 

More Clipboard Control

Clipboard stores information in multiple formats so we can transfer data between applications using different formats. When reading information from the clipboard with Delphi's TClipboard class, we are limited to standard clipboard formats: text, pictures, and metafiles.

Suppose you're working between two different Delphi applications; how would you define custom clipboard format in order to send and receive data between those two programs? For the purpose of exploration, let's say you are trying to code a Paste menu item. You want it to be disabled when there is no text in the clipboard (as an instance).

Since the entire process with the clipboard takes place behind the scenes, there is no method of TClipboard class that will inform you when some change in the content of the clipboard has taken place. The idea is to hook in the clipboard notification system, so you're able to access and respond to events when the clipboard changes.

To enjoy more flexibility and functionality, dealing with clipboard change notifications and custom clipboard formats -- listening to the Clipboard -- is necessary.

Format
mla apa chicago
Your Citation
Gajic, Zarko. "Basic Clipboard Operations (Cut/Copy/Paste) in Delphi." ThoughtCo, Feb. 16, 2021, thoughtco.com/basic-clipboard-operations-cut-copy-paste-1058406. Gajic, Zarko. (2021, February 16). Basic Clipboard Operations (Cut/Copy/Paste) in Delphi. Retrieved from https://www.thoughtco.com/basic-clipboard-operations-cut-copy-paste-1058406 Gajic, Zarko. "Basic Clipboard Operations (Cut/Copy/Paste) in Delphi." ThoughtCo. https://www.thoughtco.com/basic-clipboard-operations-cut-copy-paste-1058406 (accessed March 19, 2024).