views:

164

answers:

1

Some code I am working on uses COleDataSource::CacheGlobalData, passing as CF_TEXT an HGLOBAL pointing to some memory allocated for the text. I want to also add a numeric value, so ythe drop-target can access either the text or numeric values.

How can this easily be done? Can a 2nd CacheGlobalData call be made with a different CF_ value? And since I only want to pass an integer (DWORD) can I avoid having to allocate a messy HGLOBAL?

Or do I have to encode all the data i want to send into a single chunk of memory? Does CacheGlobalData only let be attach one object to the drag event?

A: 

You can call CacheGlobalData multiple times. For each clipboard format, the clipboard stores the last value set by CacheGlobalData. For example, IE stores data in CF_UNICODETEXT, CF_TEXT and CF_HTML formats when you drag a paragraph of text. Generally an application should provide data in as many formats as possible so more applications can recognize the data.

To avoid data lose in round trips, clipboard formats that contain the most information should be placed on the clipboard first, followed by less descriptive formats. For example, CF_HTML first, CF_UNICODETEXT second and CF_TEXT last.

You probably need to register your own format for the numeric value and modify your drop target application that can understand your own format. None of the standard formats take a numeric value.

Clipboard data must be stored in global memory. If you don't like that, you can pass NULL and handle WM_RENDERFORMAT or WM_RENDERALLFORMATS messages to provide data when needed, but for a DWORD it is not worth the effort.

Sheng Jiang 蒋晟
So there is no built-in type for passing a single DWORD? That seems really strange, it would save loads of hassle when you just want to pass an ID around. I imagine you could use the HGlobal handle to pass a single DWORD value. I don't think that's a great idea, but is there a reason it wouldn't work?
John
Other programs would be misled (by your format declaration) to believe it is a memory handle and dragging over them may cause unexpected behavior. You don't have source code for them so you don't know if all of them are checking data integrity before processing the drag over.
Sheng Jiang 蒋晟