Is it possible, and if so how would you go about implementing your own clipboard?
By this I mean be able to Copy and Paste anything to and from it just like the Windows clipboard does, but without actually interfering with the system clipboard.
To give a better idea this is what I tried:
uses
ClipBrd;
...
procedure TMainForm.actCopyExecute(Sender: TObject);
var
MyClipboard: TClipboard;
begin
MyClipboard := TClipboard.Create;
try
MyClipboard.AsText := 'Copy this text';
finally
MyClipboard.Free;
end;
end;
That works in that it will copy the string “Copy this text” to the clipboard, but it overwrites whatever was on the Windows clipboard.
The above must just create an instance of the Windows clipboard, not actually creating your own.
Note that the custom clipboard could hold any data not just plain text. It should work just the same as the Windows clipboard, but without interfering with it (losing whatever was on it).
How could this be achieved?
Thanks.
Your question is confusing; you say you want to do it without affecting the system clipboard, but then (from your own comment to your question) you seem to be wanting to implement something like MS Office’s
Paste Special.If it’s the first, as others have said you can’t do that using the
TClipboardwrapper; you have to implement your own, and passing information between applications will be very difficult.If it’s the second, you do this by using the Windows API RegisterClipboardFormat to define your own format.
To put info into the clipboard in a custom format, you have to use GlobalAlloc and GlobalLock to allocate and lock a global memory block, copy your data into that block, unlock the block using GlobalUnlock, use
TClipboard.SetAsHandleto transfer the memory block into the clipboard. You then need to then call GlobalFree to free the memory block.To retrieve things in your custom format, you do basically the same thing with a couple of steps reversed. You use GlobalAlloc/GlobalLock as before, use
TClipboard.GetAsHandleto retrieve the clipboard’s content, copy it into a local variable, and then call GlobalFree.Here’s an old example of putting a custom format (in this case, RTF text) into the clipboard – it’s from a newsgroup post by Dr. Peter Below of TeamB. (The code and formatting are his from the original post; I’ve not tested it or even compiled it.) Reversing the process to get it back out should be clear from my instructions on what to change above, and I leave that to you to work out. 🙂