how can i add (append) custom format to clipboard

801 Views Asked by At

I try to create a clipboard monitor program, i need to detect when data set from my app and then i can ignore them, for this work i try to add custome format to clipboard so first of all register new format with this code :

 int iii = RegisterClipboardFormat("ClipboardManagerIgnoreItem");


[DllImport("user32.dll")]
private static extern int RegisterClipboardFormat(string Format);

then before set dataobject to clipboard, set new format to dataobject like this :

 var data = dic_clip[key];//get source data 
 var data2 = data as DataObject;
 data2.SetData("ClipboardManagerIgnoreItem",false, "");//add new format 

and finally set data to clipboard :

Clipboard.SetDataObject(data2, true);

but after and also before set that when i check clipboard data in app i dont have any custom format !

var dt = data2.GetFormats(false);

whats wrong ?

1

There are 1 best solutions below

0
On

As far as I know there is no need to 'register' custom clipboard types. These types are just strings, and applications will only take the types they know anyway. If you want to put custom data from your own program on the clipboard, you can either use a class that uses the [Serializable] attribute, and work by object type, or just put it on the clipboard as raw byte stream.

This answer details how to do both of those. The original example is a Byte[] object, but any serializable class should work, and in your case, I assume you'll want a custom class.

I personally never tried recycling the DataObject I got from the clipboard and only add new stuff to it, but I guess that could work. Other programs will clear it, of course, but I assume that's the point, and you do this to indicate whether the current clipboard data has already been scanned by your tool. I can see how that'd be a valid method.