C#中從Clipboard儲存獲取資料的方法

iDotNetSpace發表於2009-04-20
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);
}
}
}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-591024/,如需轉載,請註明出處,否則將追究法律責任。

相關文章