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);
CloseClipboard();
}
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.)