The example in this article describes how C # operations Clipboard read data in the Clipboard. Share to everyone for your reference. The specific analysis is as follows:
1 Customize a class and ensure it is serializable: Implement the ISerializable interface, or use the [Serializable] tag (if there is a parent class, the parent class will also need to be marked; you can [NonSerialized ()] mark a field in the class that you do not want to serialize)
2 registering a custom data format: Calling a static method Dataformats.getformat ()
3 Save data to Clipboard: Using the IDataObject interface, create a data object and set the data; call the Clipboard.setdataobject () method
4 Get data from clipboard: Call the GetDataPresent () of the DataObject instance to ensure that the data format is compatible with the application; Call IDataObject's GetData () method to get the data
Sample programs:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108-109 |
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")] Priv Ate static extern IntPtr SetClipboardViewer (INTPTR hwnd); [System.Runtime.InteropServices.DllImport ("user32")] private static extern IntPtr Changeclipboardchain (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) {//Get the next window handle to the observation chain NEXTCLIPHW nd = SetClipboardViewer (this. Handle); } protected override void WndProc (ref System.Windows.Forms.Message m) { Switch (m.msg) {case Wm_drawclipboard://Pass 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 image if (Idata.getdatapresent (Dataformats.bitmap)) {pictureBox1.Image = Clipboard.getimage (); MyItem Item = new myitem (); Item. CopyToClipboard (); //Detect the custom type 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 Form1_Closed (object sender, System.EventArgs e) {//Deletes this observation window from the observation chain (first argument: handle to the window to be deleted;//second argument: Handle to the next window in the watch chain ) Changeclipboardchain (this. Handle, Nextcliphwnd); Pass the change message Wm_changecbchain message 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); } } } |
I hope this article will help you with your C # programming.