剪贴板上一般放置的都是由DataFormats类定义好的数据类型,其实我们也可以定义自己的数据类型。
创建自己写的类的对象,再将该对象在程序与剪贴板之间传来传去,挺有意思哦
剪贴板上一般放置的都是由DataFormats类定义好的数据类型,其实我们也可以定义自己的数据类型。
创建自己写的类的对象,再将该对象在程序与剪贴板之间传来传去,挺有意思哦
代码中已有注释,不再做说明。
先创建一个类,用作表达数据信息:
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
1
using System;
2![](/Images/OutliningIndicators/None.gif)
3
namespace SimpleTest
4![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
5![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
6
/// UserInfo 的摘要说明。
7
/// </summary>
8
[Serializable]//必须给此类加上这个标志,表明此类是可以序列化的(即可以将此类创建的对象转换为数据流)
9
public class UserInfo
10![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
11
public UserInfo()
12![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
13
//
14
// TODO: 在此处添加构造函数逻辑
15
//
16
}
17
public string Name = "余智学";
18
public bool IsMan = true;
19
public int Age = 20;
20
public DateTime Birthday = new DateTime(1986,1,1);
21
}
22
}
23![](/Images/OutliningIndicators/None.gif)
24![](/Images/OutliningIndicators/None.gif)
实现功能的窗体:
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Data.SqlClient;
![](/Images/OutliningIndicators/None.gif)
namespace SimpleTest
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private System.Windows.Forms.Button btnCopyToClipboard;
private System.Windows.Forms.Button btnPasteFromClipboard;
private System.Windows.Forms.TextBox txtName;
private System.Windows.Forms.RadioButton rdoIsMan;
private System.Windows.Forms.NumericUpDown numAge;
private System.Windows.Forms.DateTimePicker dtpBirthday;
private System.Windows.Forms.RadioButton rdoIsWoman;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
![](/Images/OutliningIndicators/InBlock.gif)
public Form1()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
![](/Images/OutliningIndicators/InBlock.gif)
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if( disposing )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (components != null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.btnCopyToClipboard = new System.Windows.Forms.Button();
this.btnPasteFromClipboard = new System.Windows.Forms.Button();
this.txtName = new System.Windows.Forms.TextBox();
this.rdoIsMan = new System.Windows.Forms.RadioButton();
this.rdoIsWoman = new System.Windows.Forms.RadioButton();
this.numAge = new System.Windows.Forms.NumericUpDown();
this.dtpBirthday = new System.Windows.Forms.DateTimePicker();
((System.ComponentModel.ISupportInitialize)(this.numAge)).BeginInit();
this.SuspendLayout();
//
// btnCopyToClipboard
//
this.btnCopyToClipboard.Location = new System.Drawing.Point(84, 216);
this.btnCopyToClipboard.Name = "btnCopyToClipboard";
this.btnCopyToClipboard.Size = new System.Drawing.Size(104, 23);
this.btnCopyToClipboard.TabIndex = 0;
this.btnCopyToClipboard.Text = "复制到剪贴板";
this.btnCopyToClipboard.Click += new System.EventHandler(this.btnCopyToClipboard_Click);
//
// btnPasteFromClipboard
//
this.btnPasteFromClipboard.Location = new System.Drawing.Point(260, 216);
this.btnPasteFromClipboard.Name = "btnPasteFromClipboard";
this.btnPasteFromClipboard.Size = new System.Drawing.Size(104, 23);
this.btnPasteFromClipboard.TabIndex = 0;
this.btnPasteFromClipboard.Text = "从剪贴板粘贴";
this.btnPasteFromClipboard.Click += new System.EventHandler(this.btnPasteFromClipboard_Click);
//
// txtName
//
this.txtName.Location = new System.Drawing.Point(156, 48);
this.txtName.Name = "txtName";
this.txtName.Size = new System.Drawing.Size(120, 21);
this.txtName.TabIndex = 1;
this.txtName.Text = "";
//
// rdoIsMan
//
this.rdoIsMan.Checked = true;
this.rdoIsMan.Location = new System.Drawing.Point(160, 88);
this.rdoIsMan.Name = "rdoIsMan";
this.rdoIsMan.Size = new System.Drawing.Size(48, 24);
this.rdoIsMan.TabIndex = 2;
this.rdoIsMan.TabStop = true;
this.rdoIsMan.Text = "男";
//
// rdoIsWoman
//
this.rdoIsWoman.Location = new System.Drawing.Point(232, 88);
this.rdoIsWoman.Name = "rdoIsWoman";
this.rdoIsWoman.Size = new System.Drawing.Size(56, 24);
this.rdoIsWoman.TabIndex = 2;
this.rdoIsWoman.Text = "女";
//
// numAge
//
this.numAge.Location = new System.Drawing.Point(156, 128);
this.numAge.Name = "numAge";
this.numAge.Size = new System.Drawing.Size(128, 21);
this.numAge.TabIndex = 3;
//
// dtpBirthday
//
this.dtpBirthday.Location = new System.Drawing.Point(156, 168);
this.dtpBirthday.Name = "dtpBirthday";
this.dtpBirthday.Size = new System.Drawing.Size(128, 21);
this.dtpBirthday.TabIndex = 4;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(448, 325);
this.Controls.Add(this.dtpBirthday);
this.Controls.Add(this.numAge);
this.Controls.Add(this.rdoIsMan);
this.Controls.Add(this.txtName);
this.Controls.Add(this.btnCopyToClipboard);
this.Controls.Add(this.btnPasteFromClipboard);
this.Controls.Add(this.rdoIsWoman);
this.KeyPreview = true;
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.numAge)).EndInit();
this.ResumeLayout(false);
![](/Images/OutliningIndicators/InBlock.gif)
}
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Application.Run(new Form1());
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 根据窗体上各个控件的值创建UserInfo对象
/// </summary>
/// <returns></returns>
private UserInfo CreateObj()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
UserInfo obj = new UserInfo();
obj.Name = this.txtName.Text;
obj.IsMan = this.rdoIsMan.Checked;
obj.Age = (int)this.numAge.Value;
obj.Birthday = this.dtpBirthday.Value;
return obj;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 在窗体上显示UserInfo对象
/// </summary>
/// <param name="obj"></param>
private void ShowObjInForm(UserInfo obj)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(obj != null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.txtName.Text = obj.Name;
this.rdoIsMan.Checked = obj.IsMan;
this.rdoIsWoman.Checked = !obj.IsMan;
this.numAge.Value = obj.Age;
this.dtpBirthday.Value = obj.Birthday;
}
}
private void btnCopyToClipboard_Click(object sender, System.EventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
UserInfo obj = this.CreateObj();
//申请注册一个新的Clipboard数据格式
DataFormats.Format UserInfoFormat = DataFormats.GetFormat("UserInfo");
//将对象包容到DataObj中
IDataObject myDataObj = new DataObject(obj);
//复制到剪贴板,并在退出程序时仍然保存
Clipboard.SetDataObject(myDataObj,true);
MessageBox.Show("对象已复制到剪贴板!");
}
private void btnPasteFromClipboard_Click(object sender, System.EventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//从剪贴板中提取对象
IDataObject retObj = Clipboard.GetDataObject();
//判断剪贴板上的数据是否是UserInfo类型的
if(retObj.GetDataPresent("SimpleTest.UserInfo"))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//获取UserInfo对象
UserInfo obj = retObj.GetData("SimpleTest.UserInfo") as UserInfo;
//在窗体上显示对象
this.ShowObjInForm(obj);
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MessageBox.Show("不支持的数据格式!");
}
}
private void Form1_Load(object sender, System.EventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ShowObjInForm(new UserInfo());
}
}
}