本文實例講述了C#操作Clipboard讀取剪切板中數據的方法。分享給大家供大家參考。具體分析如下:
1 自定義一個類,並且保證它的可序列化的:實現ISerializable接口;或者用[Serializable]標記(如果有父類,則父類也需要被標記;可以[NonSerialized()]標記類中不想被序列化的字段)
2 注冊自定義數據格式:調用靜態方法DataFormats.GetFormat()
3 保存數據到clipboard:利用IdataObject接口,創建一個數據對象,並設置數據;調用Clipboard.SetDataObject()方法
4 從clipboard獲取數據:調用DataObject 實例的GetDataPresent()保證數據格式與應用程序兼容;調用IDataObject 的GetData()方法獲取數據
示例程序:
?
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 43 44 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 84 85 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")] private 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) { //獲得觀察鏈中下一個窗口句柄 NextClipHwnd = SetClipboardViewer(this.Handle); } protected override void WndProc(ref System.Windows.Forms.Message m) { switch (m.Msg) { case WM_DRAWCLIPBOARD: //將WM_DRAWCLIPBOARD消息傳遞到下一個觀察鏈中的窗口 SendMessage(NextClipHwnd, m.Msg, m.WParam, m.LParam); IDataObject iData = Clipboard.GetDataObject(); //檢測文本 if (iData.GetDataPresent(DataFormats.Text) | iData.GetDataPresent(DataFormats.OemText)) { this.richTextBox1.Text = (String)iData.GetData(DataFormats.Text); } //檢測圖像 if (iData.GetDataPresent(DataFormats.Bitmap)) { pictureBox1.Image = Clipboard.GetImage(); MyItem item = new MyItem(); item.CopyToClipboard(); } //檢測自定義類型 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) { //從觀察鏈中刪除本觀察窗口(第一個參數:將要刪除的窗口的句柄; //第二個參數:觀察鏈中下一個窗口的句柄 ) ChangeClipboardChain(this.Handle, NextClipHwnd); //將變動消息WM_CHANGECBCHAIN消息傳遞到下一個觀察鏈中的窗口 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); } } }希望本文所述對大家的C#程序設計有所幫助。