开发者

C++: How do i copy multiple objects to the clipboard and extract them after it?

I need to copy a string and a bitmap to the clipboard. I have implemented copying of a string:

if(OpenClipboard(NULL))
{
HGLOBAL clipbuffer;
char * buffer;
EmptyClipboard();
clipbuffer = GlobalAlloc(GMEM_DDESHARE, strlen(source)+1);
buffer = (char*)GlobalLock(clipbuffer);
strcpy(buffer, LPCSTR(source));
GlobalUnlock(clipbuffer);
SetClipboardData(CF_TEXT,clipbuffer);
Cl开发者_运维百科oseClipboard();
}

I know how to copy image too, but i have to copy these two objects in one time (to get them in other process). Please advice


If you are asking about how to put an image and string onto the clipboard such that if you pasted into Microsoft Word then both would appear, you almost certainly want to put data on the clipboard in Rich Text Format.

I can't offhand find a good example of code that does this at a low-level using CF_RTF. An easier high-level way would be to populate an invisible Rich Text control with what you want, select it, and fire off a copy. I personally would use Qt to do it:

http://doc.qt.nokia.com/latest/qtextedit.html#copy

You can register a custom format...

http://msdn.microsoft.com/en-us/library/ms649013(v=vs.85).aspx#_win32_Registered_Clipboard_Formats

...but then no other programs would be able to interpret the image/text combo unless they were recompiled to take your new format into account. Which pretty much undermines the point of using the clipboard in the first place. Though you could provide means for RenderFormat so it will paste as a bitmap in bitmap-like programs and as text in text-like programs.

The way your question was worded you mentioned processes explicitly, and I was confused that you might be using the clipboard--which conceptually belongs to the user--to move arbitrary data between cooperating programs. There are other ways of doing that, like shared memory:

http://msdn.microsoft.com/en-us/library/aa366551(v=vs.85).aspx

(Note: If you felt like being really silly and have written both programs and don't want to register a format...you could Base64 encode the bitmap as a string, and then glue it together with the text to make a single string you pass through. Then split it apart and decode it on the receiving end.)


This is a rather difficult feat, as putting two objects of different kind into a single data structure (the clipboard can hold only one thing at a time) creates a composite, which the program on the other end probably will not understand. Unless you programmed that program, too and thus can add the additional logic. Sometimes the recieving program understands hypertext, so copying some hypertext that links to the image may do the trick, too. Similar goes for richtext.

Another approach — but this requires your program being shipped with every document the clipboard contents have been copied into — is putting an OLE object instance in the clipboard; the recieving program, if it understands OLE can then embedd your program to show that compound data structure.

If however the only thing you want to do is copying just an image and just an bitmap at once, then you're pretty much screwed, since the clipboard simply can not do it. When you select a piece of text with images in it, it usually gets copied as hyper-/richtext.


The answer depends on where're you pasting to, and what do you want to be pasted.

If you need to paste an image with text label then +1 for RTF. It's really simple text-based format: if you only need a line of text and a small image, then you should be able to build it e.g. with a dozen CStringA::AppendFormat calls (don't forget to encode non-ASCII characters as "\uXXXX"). I usually use Windows-builtin "Wordpad" application to create and format the document, then I use that tiny RTF as the template for creating RTFs from my own data (Microsoft Word saves bigger and more complex RTF documents). Wikipedia says RTF supports 8 image formats including jpeg and png.

If however you need to paste 2 files (one txt another e.g. jpeg) then you should either write them to the HDD first, or you should mess with shell data objects, implement IDataObject and IAsyncOperation interfaces, and so on.


You can definitely do this. Copy the bitmap the the clipboard, as usual. Then without clearing the clipboard, call SetClipboardData again to put the text on there. You'll have a "frankenstein" clipboard now, that can be pasted as either text or bitmap, to get the data in either format. I had an interesting situation about 10 years ago where a very popular HTML editor was failing to clear the clipboard (at all) when copying data. So if you had previously copied a bitmap, and then started using this editor, and the you pasted into some other app, you'd get completely different results depending on whether you pasted into a text-based app (like notepad), a graphics program (like paint), or something that could go either way (like Word).


You can use CF_DIB to put your image on clipboard. When you need to recieve the data use CF_TEXT or CF_DIB format.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜