博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Clipboard with Custom Clipboard Formats - Delphi
阅读量:6947 次
发布时间:2019-06-27

本文共 4665 字,大约阅读时间需要 15 分钟。

Do you know that, for example, MS Word has it's own format for storing data to the clipboard?

Large number of applications paste information in other formats, for example Rich Text Format, HTML format and so forth.
Why not use Delphi to create and store our specific formats (like records) in the clipboard.
This can be very useful in moving data between two Delphi applications.
Let's say we have a record type called TMember, declared as:

type   TMember = record     Name : string;     eMail : string;     Posts : Cardinal;   end;

We want to paste a variable of TMember type to the clipboard.

Before we can write information to the clipboard in a specific format, the format must be registered.
We register a new clipboard format by calling the RegisterClipboardFormat API function and
specifying a unique name for your new format - this enables us to copy and paste any type of data we want.

First we have to register the CF_TMember format (that holds TMember type variable values),

in the OnCreate procedure of the main form, so that we can use it later on during clipboard calls.
Newly created clipboard format will be named:
'OurFormat' and it will hold data from DelphiGuide variable filled in the OnCreate event and declared at the form level.

Then we send our data to the clipboard.

Sending custom format data to the clipboard is coded in the following way:

  1. Open the clipboard with OpenClipboard.
  2. Empty current contents of the clipboard with EmptyClipboard.
  3. Allocate and lock a global memory block.
  4. Copy data into the global memory block.
  5. Unlock the block.
  6. Transfer the block to the Clipboard with SetClipboardData. (Windows now controls the block).
  7. Close the clipboard with CloseClipboard. (So other programs can access it).
var DelphiGuide: TMember; ...     MemberPointer : ^TMember;     MemberHandle : HGLOBAL; ...  //fill some dummy data  DelphiGuide.Name := 'Zarko Gajic';  DelphiGuide.eMail := 'delphi@aboutguide.com';  DelphiGuide.Posts := 15;   OurFormat := RegisterClipboardFormat('CF_TMember') ;   if OpenClipboard(Handle) then  begin    EmptyClipboard;     MemberHandle := GlobalAlloc(GMEM_DDESHARE or GMEM_MOVEABLE, SizeOf(TMember)) ;    MemberPointer := GlobalLock(MemberHandle) ;     MemberPointer^.Name := DelphiGuide.Name;    MemberPointer^.eMail := DelphiGuide.eMail;    MemberPointer^.Posts := DelphiGuide.Posts;     GlobalUnLock(MemberHandle) ;    SetClipboardData(OurFormat,MemberHandle) ;    CloseClipboard() ;  end;

Again, to act whenever the contents of the clipboard changes we need to respond to a WM_DrawClipboard message.

We use the HasFormat function to find out whether the clipboard contains data encoded in a specific format.

Getting custom format data from the clipboard is coded in the following way:

  1. Open the clipboard with OpenClipboard.
  2. Get a handle to the clipboard data with GetClipboardData.
  3. Allocate and lock a memory block to hold the clipboard data.
  4. Lock the clipboard data block.
  5. Copy data from the clipboard data block to the program's data block
    (The data can now be manipulated by the application).
  6. Unlock both memory blocks.
  7. Close the clipboard with CloseClipboard.
if Clipboard.HasFormat(OurFormat) then begin  if OpenClipboard(Handle) then  begin   MemberInClip := GetClipboardData(OurFormat) ;   MemberPointer := GlobalLock(MemberInClip) ;   with AMember do   begin    Name := MemberPointer^.Name;    eMail := MemberPointer^.eMail;    Posts := MemberPointer^.Posts;   end;   GlobalUnLock(MemberInClip) ;   CloseClipboard() ;   with Memo1.Lines do   begin    Clear;    Add('Clipboard has TMember data:') ;    Add(AMember.Name) ;    Add(AMember.email) ;    Add(IntToStr(AMember.Posts)) ;   end;  end; end;

Note:

in normal situation we'll have one Delphi application sending data to the clipboard and another Delphi application getting data from it.
Since this is the demo project we have only one application running - we send custom formatted data to the clipboard
in the OnCreate event for the form. In the same form, we are handling the clipboard change notification, therefore when we start our project, Memo1 component is filled with data from the clipboard - i.e. the data we are sending when the form is created.
Are You in Control Now?

That's it. We have the clipboard working just as we would like it to.

No one can send data to the clipboard without us knowing that something is going on.
Even more: we can copy and paste program-specific data to the clipboard.
However, some questions stay unanswered:
What is the best way of sending an array of TMember variables to another Delphi application;
what will happen if some other application is register 'CF_TMember' with some other format, and so on.

 

转载地址:http://pnenl.baihongyu.com/

你可能感兴趣的文章
逻辑DG ORA-16240: Waiting for logfile
查看>>
ORACLE系列脚本3:救命的JOB处理脚本
查看>>
STP
查看>>
yii 一些引用路径的方法
查看>>
vue图片上传相关(持续更新)
查看>>
java内存简单总结
查看>>
实现windows server 2008 R2多用户同时登陆或者同一用户名同时登陆
查看>>
PMD 插件的安装和使用
查看>>
利用JavaScript生成二维码并且中间有logo
查看>>
泛型小例子
查看>>
译文:C#中的弱事件(Weak Events in C#)
查看>>
抽象工厂模式
查看>>
Maven
查看>>
Unix-Linux 编程实践教程 第八章 小结
查看>>
linux下ElasticSearch安装及问题
查看>>
2019测试指南-web应用程序安全测试(二)指纹Web应用程序
查看>>
Quartus Prime 下载程序到FPGA流程
查看>>
php instanceof 运算符
查看>>
5月3日云栖精选夜读丨寒武纪重磅发布首款AI云芯片,阿里专家告诉你必须注意的Java编程细节...
查看>>
机器学习从业人员到底做什么?
查看>>