Mega Code Archive

 
Categories / Delphi / Examples
 

Clipboard

This information from http://delphi.about.com/compute/delphi/library/weekly/aa092199.htm USING THE CLIPBOARD WITH DELPHI CODE 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 The string value of the AsText property is limited to 255 characters. To set and retrieve more than 255 characters, use the SetTextBuf and GetTextBuf Clipboard methods. Naturally, to retrieve the text information from the Clipboard we'll use uses ClipBrd; ... SomeStringData := Clipboard.AsText Note: The Clipboard must contain a string or an exception occurs. To determine if the Clipboard contains a string type we'll use the the HasFormat method. uses ClipBrd; ... if Clipboard.HasFormat(CF_TEXT) then SomeStringData := Clipboard.AsText else ShowMessage('No text in the Clipboard'); 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 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: uses ClipBrd; ... 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); begin if Clipboard.HasFormat(CF_BITMAP) then Image1.Picture.Bitmap.Assign(Clipboard); end;