C#操作Clipboard讀取剪切板中數據實例詳解。本站提示廣大學習愛好者:(C#操作Clipboard讀取剪切板中數據實例詳解)文章只能為提供參考,不一定能成為您想要的結果。以下是C#操作Clipboard讀取剪切板中數據實例詳解正文
本文實例講述了C#操作Clipboard讀取剪切板中數據的辦法。分享給年夜家供年夜家參考。詳細剖析以下:
1 自界說一個類,而且包管它的可序列化的:完成ISerializable接口;或許用[Serializable]標志(假如有父類,則父類也須要被標志;可以[NonSerialized()]標志類中不想被序列化的字段)
2 注冊自界說數據格局:挪用靜態辦法DataFormats.GetFormat()
3 保留數據到clipboard:應用IdataObject接口,創立一個數據對象,並設置數據;挪用Clipboard.SetDataObject()辦法
4 從clipboard獲得數據:挪用DataObject 實例的GetDataPresent()包管數據格局與運用法式兼容;挪用IDataObject 的GetData()辦法獲得數據
示例法式:
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#法式設計有所贊助。