Using System; Using System. Collections. Generic; Using System. ComponentModel; Using System. Data; Using System. Drawing; Using System. Linq; Using System. Text; Using System. Windows. Forms; Using System. Runtime. Serialization. Formatters. Binary; Namespace _ ClipboardTest _ { Public partial class Form1: Form { [System. Runtime. InteropServices. DllImport ("user32")] Private static extern IntPtr SetClipboardViewer (IntPtr hwnd ); [System. Runtime. InteropServices. DllImport ("user32")] Private static extern IntPtr changeconboardchain (IntPtr hwnd, IntPtr hWndNext ); [System. Runtime. InteropServices. DllImport ("user32")] Private static extern int SendMessage (IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam ); Const int WM_DRAWCLIPBOARD = 0x308; Const int WM_CHANGECBCHAIN = 0x30D; Public Form1 () { InitializeComponent (); } Private void Form1_Load (object sender, EventArgs e) { // Obtain the next window handle in the observation chain NextClipHwnd = SetClipboardViewer (this. Handle ); } Protected override void WndProc (ref System. Windows. Forms. Message m) { Switch (m. Msg) { Case WM_DRAWCLIPBOARD: // Pass the WM_DRAWCLIPBOARD message to the window in the next observation chain SendMessage (NextClipHwnd, m. Msg, m. WParam, m. LParam ); IDataObject iData = Clipboard. GetDataObject (); // Detect text If (iData. GetDataPresent (DataFormats. Text) | iData. GetDataPresent (DataFormats. OemText )) { This. richTextBox1.Text = (String) iData. GetData (DataFormats. Text ); } // Detect Images If (iData. GetDataPresent (DataFormats. Bitmap )) { PictureBox1.Image = Clipboard. GetImage (); MyItem item = new MyItem (); Item. CopyToClipboard (); } // Detect custom types If (iData. GetDataPresent (typeof (MyItem). FullName )) { // MyItem item = (MyItem) iData. GetData (typeof (MyItem). FullName ); MyItem item = GetFromClipboard (); If (item! = Null) { This. richTextBox1.Text = item. ItemName; } } Break; Default: Base. WndProc (ref m ); Break; } } Private void formpolicclosed (object sender, System. EventArgs e) { // Delete the observation window from the observation chain (first parameter: handle of the window to be deleted; // Second parameter: Observe the handle of the next window in the chain) Changeconboardchain (this. Handle, NextClipHwnd ); // Send the changed message WM_CHANGECBCHAIN to the window in the next observation chain SendMessage (NextClipHwnd, WM_CHANGECBCHAIN, this. Handle, NextClipHwnd ); } IntPtr NextClipHwnd; Protected static MyItem GetFromClipboard () { MyItem item = null; IDataObject dataObj = Clipboard. GetDataObject (); String format = typeof (MyItem). FullName; If (dataObj. GetDataPresent (format )) { Item = dataObj. GetData (format) as MyItem; } Return item; } } [Serializable] Public class MyItem { Public MyItem () { ItemName = "This is a Custom Item "; } Public string ItemName { Get {return itemName ;} } Private string itemName; Public void CopyToClipboard () { DataFormats. Format format = DataFormats. GetFormat (typeof (MyItem). FullName ); IDataObject dataObj = new DataObject (); DataObj. SetData (format. Name, false, this ); Clipboard. SetDataObject (dataObj, false ); } } } |