How to reliably detect RICHTEXT format on clipboard?

流过昼夜 提交于 2021-02-07 17:22:55

问题


Embarcadero RAD Studio VCL has the TClipboard.HasFormat Method, with a usage e.g. Clipboard.HasFormat(CF_TEXT) or Clipboard.HasFormat(CF_BITMAP) etc..

But I did not find any supported CF_RTF or CF_RICHTEXT format-descriptor which indicates a rich-text format in the clipboard.

So I created some formatted text in Microsoft WordPad and copied it to the clipboard. Then I used a clipboard-spy program to inspect the formats on the clipboard:

This lists 3 RichText formats with the format-descriptors C078, C16B and C1A5.

Are these format-descriptors universal or dependent from the individual system or from the current situation? I.e., can I generally use Clipboard.HasFormat($C078) to detect any RichText format on the clipboard? Or is there another method?


回答1:


Can I generally use Clipboard.HasFormat($C078) to detect any RichText format on the clipboard?

No, You need to register the RTF clipboard format via RegisterClipboardFormat function. The returned value is generated by the system and may vary.

Registers a new clipboard format. This format can then be used as a valid clipboard format.

If a registered format with the specified name already exists, a new format is not registered and the return value identifies the existing format. This enables more than one application to copy and paste data using the same registered clipboard format.

var
  CF_RTF: UINT;
...
initialization
  CF_RTF := RegisterClipboardFormat('Rich Text Format');

Then check for:

if Clipboard.HasFormat(CF_RTF) then ...
{ or // if Windows.IsClipboardFormatAvailable(CF_RTF) then ... }

Edit: After reading the documentation: How to Use Rich Edit Clipboard Operations

The constant CF_RTF is already declared in RichEdit unit as:

CF_RTF                 = 'Rich Text Format';
CF_RTFNOOBJS           = 'Rich Text Format Without Objects'; 
CF_RETEXTOBJ           = 'RichEdit Text and Objects';

So it might be a better idea to use other naming for the returned value of RegisterClipboardFormat. e.g.

uses RichEdit;
...
var
  CF_RICHTEXT: UINT;
...
initialization
  CF_RICHTEXT := RegisterClipboardFormat(RichEdit.CF_RTF);

And:

if Clipboard.HasFormat(CF_RICHTEXT) then ...

Note: There are already a few reserved system clipboard formats such as CF_TEXT (=1), CF_BITMAP(=2) etc ... but the "CF_RTF" or "CF_RICHTEXT" is not one of them. it is a custom format used by the RICHEDIT common control, and registered via RegisterClipboardFormat as already mentioned.



来源:https://stackoverflow.com/questions/45837425/how-to-reliably-detect-richtext-format-on-clipboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!