How to insert arbitrary data into TMemo?

Copying and pasting text is quite simple since it is built into TMemo, but it seems that it can only process text. It seems to me that any data can be represented as a string. If I copy some arbitrary data from another program and want to paste it into TMemo, how do I get Delphi to accept it as an unprocessed string?

+2
source share
4 answers

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

  • Do you want to use any other application (you do not have code for a regular application) and copy something into it
  • Want to paste this copied data, which can be in any format, including non-text, as text in your note.

It is right? If so, you cannot use Clipboard.AsText -, which returns text only if the clipboard has data with the format CF_TEXT . You will need to use the clipboard APIs directly. The clipboard stores data with a format code, and you can get a pointer to this data and size, and treat it as a string or as you want.

So:

  • Decide which format you want to insert. You can iterate over all the formats currently on the clipboard through EnumClipboardFormats or use one of the predefined constants (for text, images, audio, etc. ..) The clipboard can store data in many formats at the same time, so you can choose which one you are using.
  • If the data in this format is on the clipboard, open it . Make sure you complete this code in try/finally and close the clipboard in the finally clause. If you do not 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 data descriptor in this format. Data about (or given if you later run Copy) is allocated through GlobalAlloc , so you need to lock the handle to get a pointer to it through GlobalLock (and once done, open GlobalUnlock .) Data stored in the clipboard does not free it after of how you used it. To find the size of this data in bytes, use GlobalSize .
  • This gives you a pointer to data of known size. At this point you can do whatever you want. Treating it as a string is one option. Since your application does not own the data, you should copy it, and not directly manipulate it.

Your code should know that the data has a certain size and probably will not end with zeros (or may have zeros in it), so when converting to a string, make sure that you do not overflow the buffer. You can encode it to avoid NULL , etc. If you want to get more detailed information about something like this, then probably you should ask another question (or look for encoding arbitrary data as a string). The simplest task would be to copy the data to a buffer of size + 1, set the last byte to zero, iterate through each byte, except the very last and for non-printable characters (byte value <32), change it to "." or some other character. Then pass the pointer to this buffer to the AnsiString constructor listed as PAnsiChar . (This ensures that your data is treated as a string of characters of byte size - it should be borne in mind if you use D2009 +, since the native string type is Unicode.) An alternative string type to use is RawByteString . When you have a line, add it to your note.

There is a good example (in C, sorry) of inserting data of a specific format in MSDN . You can use this as a starting point by adding your own personal attitude to the data. Insert as a string is probably not the best way to view arbitrary binary data - you can use the hex editor component or some other visualizer to better view the data.

+6
source

Mason, I'm not sure I understand your answer, but yes, you can use Clipboard.AsText, as avar said, just add clbrbrd to the uses section. Please note that if you have a char zero somewhere in your string, then KAZAM your string will be inserted from the beginning to zero char # 0. Another approach would be to use memory mapped files or messages to send data between applications.

+2
source

You tried to use clipbaord.astext between your tmemo and "another program"

+1
source

I'm not sure that your statement "any kind of data can be represented as a string" makes sense. Binary data can have embedded zeros, which interferes with the processing of data as a string. And what is the meaning of a string representation of, for example, a bitmap?

This is an application that sets the clipboard to determine the format of information on the clipboard. If the application is well thought out to provide a textual representation of non-textual information (for example, the file name for the image, if any), you can use this string information. Otherwise, it is unclear what to insert this information into TMemo.

0
source

Source: https://habr.com/ru/post/1390503/


All Articles