如何将任意数据粘贴到TMemo中? [英] How can I paste arbitrary data into a TMemo?

查看:134
本文介绍了如何将任意数据粘贴到TMemo中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

复制和粘贴文本非常容易,因为它是TMemo内置的,但似乎只能处理文本.在我看来,任何类型的数据都可以表示为字符串.如果我从另一个程序复制了一些任意数据并将其粘贴到TMemo中,如何让Delphi接受它作为原始字符串?

Copying and pasting text is easy enough, since it's built into TMemo, but it seems that it can only handle text. Seems to me that any kind of data can be represented as a string, though. If I copy some arbitrary data from another program and want to paste it into a TMemo, how do I get Delphi to accept it as a raw string?

推荐答案

如果我从另一个程序中复制了一些任意数据并将其粘贴到TMemo中,我该如何让Delphi接受它作为原始数据字符串?" 为澄清您的问题,您:

"If I copy some arbitrary data from another program and want to paste it into a TMemo, how do I get Delphi to accept it as a raw string?" So to clarify your question, you:

  • 想使用任何其他应用程序(您没有代码的普通应用程序)并在其中复制某些内容
  • 想要将此复制的数据粘贴为备忘录中的文本,该数据可以是包括非文本在内的任何格式.

那是正确的吗?如果是这样,则不能使用Clipboard.AsText-仅在剪贴板上存在具有CF_TEXT格式的数据时才返回文本.您需要直接使用剪贴板API.剪贴板包含带有格式代码的数据,您可以获取指向该数据和大小的指针,然后将其视为字符串或任意使用.

Is that correct? If so, you can't use Clipboard.AsText - that only returns text if there is data with the CF_TEXT format on the clipboard. You'll need to use the clipboard APIs directly. The clipboard holds data with a format code, and you can get a pointer to that data, and a size, and treat it as a string or however you want.

所以:

  • 找出要粘贴的格式.您可以通过 ,或使用预定义的常量之一(用于文本,图像,音频等).剪贴板可以一次保存多种格式的数据,因此您可能要选择要使用的多种.
  • 如果这种格式的数据在剪贴板上,请打开.确保将此代码包装在try/finally中,并关闭finally子句中的剪贴板.如果您不关闭剪贴板,则其他应用程序将无法使用它,因此即使您的应用程序崩溃,您也希望将其关闭.
  • 致电 GetClipboardData 以获取该格式的数据的句柄.通过(a,如果您以后实现复制",则提供给(或提供给),则通过 .
  • 这为您提供了一个指向已知大小数据的指针.此时,您可以使用它做任何您想做的事情.将其视为字符串是一种选择.由于您的应用程序不拥有数据,因此您应该复制它,而不要直接对其进行操作.
  • Figure out what format you want to paste. You can iterate through all formats currently on the clipboard via EnumClipboardFormats, or use one of the predefined constants (for text, images, audio, etc.) The clipboard can hold data in many formats at once, so you may want to choose which of many you use.
  • If data in this format is on the clipboard, open it. Ensure you wrap this code in a try/finally and close the clipboard in the finally clause. If you don't close the clipboard, no other application will be able to use it, so you want it to be closed even if your application crashes.
  • Call GetClipboardData to get a handle to the data in that format. Data on (or given to, if you later implement Copy) is allocated via GlobalAlloc, so you need to lock the handle to get a pointer to it via GlobalLock (and once done, unlock with GlobalUnlock.) The data's owned by the clipboard so don't free it after you've used it. To find the size of this data in bytes, use GlobalSize.
  • This gives you a pointer to data of a known size. At this point you can do anything you want with it. Treating it as a string is one option. Since your app does not own the data you should copy it, not directly manipulate it.

您的代码应注意数据是一定大小的,并且可能不会以空值结尾(或可能包含空值),因此在转换为字符串时,请确保不会溢出缓冲区.您可以对它进行编码以避免NULL等.如果您想要更多类似的信息,可能值得提出另一个问题(或搜索将任意数据编码为字符串).最简单的方法是将数据复制到一个大小为1的缓冲区,将最后一个字节设置为null,遍历除最后一个字节之外的每个字节,对于不可打印的字符(字节值< 32),将其更改为".或其他一些字符.然后将指向此缓冲区的指针传递给AnsiString的构造函数,并转换为PAnsiChar. (这确保您的数据被视为字节大小的字符的字符串-如果使用D2009 +,则要牢记这一点,因为本机字符串类型是Unicode.)要使用的另一种字符串类型是RawByteString.有了字符串后,将其添加到备忘录中.

Your code should be aware the data is of a certain size and probably wont be null-terminated (or may have nulls in it) so when converting to a string, make sure you don't overrun the buffer. You could encode it to avoid NULLs etc. If you want more details on something like this it's probably worth asking another question (or search for encoding arbitrary data as string.) The simplest thing to do would be to copy the data into a size+1 buffer, set the last byte to null, iterate through every byte except the very last and for non-printable characters (byte value < 32) change it to "." or some other character. Then pass a pointer to this buffer to AnsiString's constructor, cast as a PAnsiChar. (This ensures your data is treated as a string of byte-size chars - worth keeping in mind if you're using D2009+ since the native string type is Unicode.) An alternative string type to use is RawByteString. Once you have a string, add it to your memo.

有一个很好的例子(用C语言表示抱歉)在MSDN上粘贴特定格式的数据.您可以以此为起点,添加对数据的自定义处理.粘贴为字符串可能不是查看任意二进制数据的最佳方法-您可以使用十六进制编辑器组件或某些其他可视化工具来更好地查看数据.

There is a good example (in C, sorry) of pasting data of a specific format on MSDN. You could use that as a starting point, adding in your own custom treatment of the data. Pasting as a string is probably not the best way to view arbitrary binary data - you could use a hex editor component or some other visualiser to give you a better view of the data.

这篇关于如何将任意数据粘贴到TMemo中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆