delphi 复制到剪贴板_Delphi中的基本剪贴板操作(剪切/复制/粘贴)

扶开诚
2023-12-01

delphi 复制到剪贴板

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.

Windows剪贴板代表从应用程序剪切,复制或粘贴的任何文本或图形的容器。 本文将向您展示如何使用TClipboard对象在Delphi应用程序中实现剪切复制粘贴功能。

剪贴板一般 ( 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.

您可能知道,剪贴板一次只能保存一份相同类型的数据,以便进行剪切,复制和粘贴。 如果我们以相同的格式将新信息发送到剪贴板,则会擦除之前的内容,但是即使将剪贴板中的内容粘贴到另一个程序中,剪贴板中的内容也会保留在剪贴板中。

TC剪贴板 ( 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.

为了在我们的应用程序中使用Windows剪贴板,我们必须将ClipBrd单元添加到项目的uses子句中,除非我们将剪切,复制和粘贴限制在已经具有对Clipboard方法的内置支持的组件上。 这些组件是TEdit,TMemo,TOLEContainer,TDDEServerItem,TDBEdit,TDBImage和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.

ClipBrd单元自动表示一个称为剪贴板的TClipboard对象。 我们将使用CutToClipboardCopyToClipboardPasteFromClipboardClearHasFormat方法来处理剪贴板操作和文本/图形操作。

发送和检索文本 ( 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:

为了将一些文本发送到剪贴板,使用了剪贴板对象的AsText属性。 例如,如果我们想将变量SomeStringData中包含的字符串信息发送到剪贴板(清除其中的任何文本),我们将使用以下代码:

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.

注意:如果只想将文本从“编辑”组件复制到剪贴板,则不必在Uses子句中包含ClipBrd单元。 TEdit的CopyToClipboard方法将编辑控件中的选定文本以CF_TEXT格式复制到剪贴板。

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.

要从剪贴板检索图形图像,Delphi必须知道在那里存储了哪种图像。 同样,要将图像传输到剪贴板,应用程序必须告诉剪贴板它正在发送哪种类型的图形。 随后是Format参数的一些可能值; Windows提供了更多剪贴板格式。

  • CF_TEXT - Text with each line ending with a CR-LF combination.

    CF_TEXT-每行以CR-LF组合结尾的文本。

  • CF_BITMAP - A Windows bitmap graphic.

    CF_BITMAP -Windows位图图形。

  • CF_METAFILEPICT - A Windows metafile graphic.

    CF_METAFILEPICT -Windows图元文件图形。

  • CF_PICTURE - An object of type TPicture.

    CF_PICTURE -TPicture类型的对象。

  • CF_OBJECT - Any persistent object.

    CF_OBJECT-任何持久对象。

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

如果剪贴板中的图像格式正确,则HasFormat方法将返回True:

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:

使用“分配”方法将图像发送(分配)到剪贴板。 例如,以下代码将位图从名为MyBitmap的位图对象复制到剪贴板:

Clipboard.Assign(MyBitmap) ;

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

通常,MyBitmap是TGraphics,T​​Bitmap,TMetafile或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:

要从剪贴板中检索图像,我们必须:验证剪贴板当前内容的格式,并使用目标对象的Assign方法:

{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.

剪贴板以多种格式存储信息,因此我们可以使用不同格式在应用程序之间传输数据。 当使用Delphi的TClipboard类从剪贴板中读取信息时,我们仅限于标准剪贴板格式:文本,图片和图元文件。

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).

假设您正在两个不同的Delphi应用程序之间工作; 您如何定义自定义剪贴板格式,以便在这两个程序之间发送和接收数据? 出于探索的目的,假设您正在尝试对“粘贴” 菜单项进行编码。 您希望在剪贴板中没有文本(例如)时禁用它。

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.

由于剪贴板的整个过程都在后台进行,因此没有TClipboard类的方法会在剪贴板内容发生某些更改时通知您。 这个想法是挂钩剪贴板通知系统,以便您能够在剪贴板更改时访问并响应事件。

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

为了享受更多的灵活性和功能,必须处理剪贴板更改通知和自定义剪贴板格式(侦听剪贴板)。

翻译自: https://www.thoughtco.com/basic-clipboard-operations-cut-copy-paste-1058406

delphi 复制到剪贴板

 类似资料: