Basic Clipboard Operations (Cut/Copy/Paste)

Using the TClipboard object

Businessman working at computer
Jupiterimages/Stockbyte/Getty Images

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 data for cut, copy and paste at one time. In general, it can hold only one piece of the same kind of data at a time.

If we send new information of the same format to the Clipboard, we wipe out what was there before. The contents of the Clipboard stays with the Clipboard even after we paste those contents into another program.


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 that have built-in support for Clipboard methods. Those components are TEdit, TMemo, TOLEContainer, TDDEServerItem, TDBEdit, TDBImage and TDBMemo.
The ClipBrd unit automatically instantiates 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) ;
    //the following line will select    //ALL the text in the edit control    {Edit1.SelectAll;}

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') ; 

To send (assign) an image to the Clipboard, we use the Assign method. 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) ;
 if Clipboard.HasFormat(CF_BITMAP) then Image1.Picture.Bitmap.Assign(Clipboard) ;

More Clipboard Control

Clipboard stores information in multiple formats so we can transfer data between applications that use 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 we have two different Delphi applications running, what do you say about defining custom clipboard format in order to send and receive data between those two programs? Suppose we are trying to code a Paste menu item - we want it to be disabled when there is no, let's say, text in the clipboard. Since the entire process with the clipboard takes place behind the scenes, there is no method of TClipboard class that will inform us that there has been some change in the content of the clipboard. What we need is to hook in the clipboard notification system, so we can get and respond to events when the clipboard changes.

If we want more flexibility and functionality we have to deal with clipboard change notifications and custom clipboard formats: Listening to the Clipboard.

mla apa chicago
Your Citation
Gajic, Zarko. "Basic Clipboard Operations (Cut/Copy/Paste)." ThoughtCo, Jul. 22, 2017, Gajic, Zarko. (2017, July 22). Basic Clipboard Operations (Cut/Copy/Paste). Retrieved from Gajic, Zarko. "Basic Clipboard Operations (Cut/Copy/Paste)." ThoughtCo. (accessed February 19, 2018).