C#操作Clipboard讀取剪切板中數(shù)據(jù)實(shí)例詳解
本文實(shí)例講述了C#操作Clipboard讀取剪切板中數(shù)據(jù)的方法。分享給大家供大家參考。具體分析如下:
1 自定義一個(gè)類,并且保證它的可序列化的:實(shí)現(xiàn)ISerializable接口;或者用[Serializable]標(biāo)記(如果有父類,則父類也需要被標(biāo)記;可以[NonSerialized()]標(biāo)記類中不想被序列化的字段)
2 注冊(cè)自定義數(shù)據(jù)格式:調(diào)用靜態(tài)方法DataFormats.GetFormat()
3 保存數(shù)據(jù)到clipboard:利用IdataObject接口,創(chuàng)建一個(gè)數(shù)據(jù)對(duì)象,并設(shè)置數(shù)據(jù);調(diào)用Clipboard.SetDataObject()方法
4 從clipboard獲取數(shù)據(jù):調(diào)用DataObject 實(shí)例的GetDataPresent()保證數(shù)據(jù)格式與應(yīng)用程序兼容;調(diào)用IDataObject 的GetData()方法獲取數(shù)據(jù)
示例程序:
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)
{
//獲得觀察鏈中下一個(gè)窗口句柄
NextClipHwnd = SetClipboardViewer(this.Handle);
}
protected override void WndProc(ref System.Windows.Forms.Message m)
{
switch (m.Msg)
{
case WM_DRAWCLIPBOARD:
//將WM_DRAWCLIPBOARD消息傳遞到下一個(gè)觀察鏈中的窗口
SendMessage(NextClipHwnd, m.Msg, m.WParam, m.LParam);
IDataObject iData = Clipboard.GetDataObject();
//檢測(cè)文本
if (iData.GetDataPresent(DataFormats.Text) | iData.GetDataPresent(DataFormats.OemText))
{
this.richTextBox1.Text = (String)iData.GetData(DataFormats.Text);
}
//檢測(cè)圖像
if (iData.GetDataPresent(DataFormats.Bitmap))
{
pictureBox1.Image = Clipboard.GetImage();
MyItem item = new MyItem();
item.CopyToClipboard();
}
//檢測(cè)自定義類型
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)
{
//從觀察鏈中刪除本觀察窗口(第一個(gè)參數(shù):將要?jiǎng)h除的窗口的句柄;
//第二個(gè)參數(shù):觀察鏈中下一個(gè)窗口的句柄 )
ChangeClipboardChain(this.Handle, NextClipHwnd);
//將變動(dòng)消息WM_CHANGECBCHAIN消息傳遞到下一個(gè)觀察鏈中的窗口
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);
}
}
}
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
C#利用Label標(biāo)簽控件模擬窗體標(biāo)題的移動(dòng)及窗體顏色不斷變換效果
Label標(biāo)簽控件相信對(duì)大家來說都不陌生,下面這篇文章主要給大家介紹了關(guān)于C#利用Label標(biāo)簽控件模擬窗體標(biāo)題的移動(dòng)及窗體顏色不斷變換效果的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。2017-12-12
c# 線程定時(shí)器 System.Threading.Timer的使用
本文主要介紹了c# 線程定時(shí)器 System.Threading.Timer的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
unity3D實(shí)現(xiàn)攝像機(jī)抖動(dòng)特效
這篇文章主要為大家詳細(xì)介紹了unity3D實(shí)現(xiàn)攝像機(jī)抖動(dòng)特效,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-01-01
C#實(shí)現(xiàn)較為實(shí)用的SQLhelper
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)較為實(shí)用SQLhelper的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10

