在剪贴板上保存自定义对象

剪贴板上一般放置的都是由DataFormats类定义好的数据类型,其实我们也可以定义自己的数据类型。
创建自己写的类的对象,再将该对象在程序与剪贴板之间传来传去,挺有意思哦

代码中已有注释,不再做说明。

先创建一个类,用作表达数据信息:

ContractedBlock.gif ExpandedBlockStart.gif
 1None.gifusing System;
 2None.gif
 3None.gifnamespace SimpleTest
 4ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 5ExpandedSubBlockStart.gifContractedSubBlock.gif /**//// <summary>
 6InBlock.gif /// UserInfo 的摘要说明。
 7ExpandedSubBlockEnd.gif /// </summary>

 8InBlock.gif [Serializable]//必须给此类加上这个标志,表明此类是可以序列化的(即可以将此类创建的对象转换为数据流)
 9InBlock.gif public class UserInfo
10ExpandedSubBlockStart.gifContractedSubBlock.gif dot.gif{
11InBlock.gif  public UserInfo()
12ExpandedSubBlockStart.gifContractedSubBlock.gif  dot.gif{
13InBlock.gif   //
14InBlock.gif   // TODO: 在此处添加构造函数逻辑
15InBlock.gif   //
16ExpandedSubBlockEnd.gif  }

17InBlock.gif  public string Name = "余智学";
18InBlock.gif  public bool IsMan = true;
19InBlock.gif  public int Age = 20;
20InBlock.gif  public DateTime Birthday = new DateTime(1986,1,1);
21ExpandedSubBlockEnd.gif }

22ExpandedBlockEnd.gif}

23None.gif
24None.gif


实现功能的窗体:

ContractedBlock.gif ExpandedBlockStart.gif
None.gifusing System;
None.gif
using System.Drawing;
None.gif
using System.Collections;
None.gif
using System.ComponentModel;
None.gif
using System.Windows.Forms;
None.gif
using System.Data;
None.gif
using System.IO;
None.gif
using System.Data.SqlClient;
None.gif
None.gif
namespace SimpleTest
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Form1 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class Form1 : System.Windows.Forms.Form
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private System.Windows.Forms.Button btnCopyToClipboard;
InBlock.gif        
private System.Windows.Forms.Button btnPasteFromClipboard;
InBlock.gif        
private System.Windows.Forms.TextBox txtName;
InBlock.gif        
private System.Windows.Forms.RadioButton rdoIsMan;
InBlock.gif        
private System.Windows.Forms.NumericUpDown numAge;
InBlock.gif        
private System.Windows.Forms.DateTimePicker dtpBirthday;
InBlock.gif        
private System.Windows.Forms.RadioButton rdoIsWoman;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 必需的设计器变量。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private System.ComponentModel.Container components = null;
InBlock.gif
InBlock.gif        
public Form1()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// Windows 窗体设计器支持所必需的
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif
InBlock.gif            
//
InBlock.gif            
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
InBlock.gif            
//
ExpandedSubBlockEnd.gif
        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 清理所有正在使用的资源。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        protected override void Dispose( bool disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if( disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (components != null
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    components.Dispose();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
base.Dispose( disposing );
ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif        
/// 此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.btnCopyToClipboard = new System.Windows.Forms.Button();
InBlock.gif            
this.btnPasteFromClipboard = new System.Windows.Forms.Button();
InBlock.gif            
this.txtName = new System.Windows.Forms.TextBox();
InBlock.gif            
this.rdoIsMan = new System.Windows.Forms.RadioButton();
InBlock.gif            
this.rdoIsWoman = new System.Windows.Forms.RadioButton();
InBlock.gif            
this.numAge = new System.Windows.Forms.NumericUpDown();
InBlock.gif            
this.dtpBirthday = new System.Windows.Forms.DateTimePicker();
InBlock.gif            ((System.ComponentModel.ISupportInitialize)(
this.numAge)).BeginInit();
InBlock.gif            
this.SuspendLayout();
InBlock.gif            
// 
InBlock.gif            
// btnCopyToClipboard
InBlock.gif            
// 
InBlock.gif
            this.btnCopyToClipboard.Location = new System.Drawing.Point(84216);
InBlock.gif            
this.btnCopyToClipboard.Name = "btnCopyToClipboard";
InBlock.gif            
this.btnCopyToClipboard.Size = new System.Drawing.Size(10423);
InBlock.gif            
this.btnCopyToClipboard.TabIndex = 0;
InBlock.gif            
this.btnCopyToClipboard.Text = "复制到剪贴板";
InBlock.gif            
this.btnCopyToClipboard.Click += new System.EventHandler(this.btnCopyToClipboard_Click);
InBlock.gif            
// 
InBlock.gif            
// btnPasteFromClipboard
InBlock.gif            
// 
InBlock.gif
            this.btnPasteFromClipboard.Location = new System.Drawing.Point(260216);
InBlock.gif            
this.btnPasteFromClipboard.Name = "btnPasteFromClipboard";
InBlock.gif            
this.btnPasteFromClipboard.Size = new System.Drawing.Size(10423);
InBlock.gif            
this.btnPasteFromClipboard.TabIndex = 0;
InBlock.gif            
this.btnPasteFromClipboard.Text = "从剪贴板粘贴";
InBlock.gif            
this.btnPasteFromClipboard.Click += new System.EventHandler(this.btnPasteFromClipboard_Click);
InBlock.gif            
// 
InBlock.gif            
// txtName
InBlock.gif            
// 
InBlock.gif
            this.txtName.Location = new System.Drawing.Point(15648);
InBlock.gif            
this.txtName.Name = "txtName";
InBlock.gif            
this.txtName.Size = new System.Drawing.Size(12021);
InBlock.gif            
this.txtName.TabIndex = 1;
InBlock.gif            
this.txtName.Text = "";
InBlock.gif            
// 
InBlock.gif            
// rdoIsMan
InBlock.gif            
// 
InBlock.gif
            this.rdoIsMan.Checked = true;
InBlock.gif            
this.rdoIsMan.Location = new System.Drawing.Point(16088);
InBlock.gif            
this.rdoIsMan.Name = "rdoIsMan";
InBlock.gif            
this.rdoIsMan.Size = new System.Drawing.Size(4824);
InBlock.gif            
this.rdoIsMan.TabIndex = 2;
InBlock.gif            
this.rdoIsMan.TabStop = true;
InBlock.gif            
this.rdoIsMan.Text = "";
InBlock.gif            
// 
InBlock.gif            
// rdoIsWoman
InBlock.gif            
// 
InBlock.gif
            this.rdoIsWoman.Location = new System.Drawing.Point(23288);
InBlock.gif            
this.rdoIsWoman.Name = "rdoIsWoman";
InBlock.gif            
this.rdoIsWoman.Size = new System.Drawing.Size(5624);
InBlock.gif            
this.rdoIsWoman.TabIndex = 2;
InBlock.gif            
this.rdoIsWoman.Text = "";
InBlock.gif            
// 
InBlock.gif            
// numAge
InBlock.gif            
// 
InBlock.gif
            this.numAge.Location = new System.Drawing.Point(156128);
InBlock.gif            
this.numAge.Name = "numAge";
InBlock.gif            
this.numAge.Size = new System.Drawing.Size(12821);
InBlock.gif            
this.numAge.TabIndex = 3;
InBlock.gif            
// 
InBlock.gif            
// dtpBirthday
InBlock.gif            
// 
InBlock.gif
            this.dtpBirthday.Location = new System.Drawing.Point(156168);
InBlock.gif            
this.dtpBirthday.Name = "dtpBirthday";
InBlock.gif            
this.dtpBirthday.Size = new System.Drawing.Size(12821);
InBlock.gif            
this.dtpBirthday.TabIndex = 4;
InBlock.gif            
// 
InBlock.gif            
// Form1
InBlock.gif            
// 
InBlock.gif
            this.AutoScaleBaseSize = new System.Drawing.Size(614);
InBlock.gif            
this.ClientSize = new System.Drawing.Size(448325);
InBlock.gif            
this.Controls.Add(this.dtpBirthday);
InBlock.gif            
this.Controls.Add(this.numAge);
InBlock.gif            
this.Controls.Add(this.rdoIsMan);
InBlock.gif            
this.Controls.Add(this.txtName);
InBlock.gif            
this.Controls.Add(this.btnCopyToClipboard);
InBlock.gif            
this.Controls.Add(this.btnPasteFromClipboard);
InBlock.gif            
this.Controls.Add(this.rdoIsWoman);
InBlock.gif            
this.KeyPreview = true;
InBlock.gif            
this.Name = "Form1";
InBlock.gif            
this.Text = "Form1";
InBlock.gif            
this.Load += new System.EventHandler(this.Form1_Load);
InBlock.gif            ((System.ComponentModel.ISupportInitialize)(
this.numAge)).EndInit();
InBlock.gif            
this.ResumeLayout(false);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 应用程序的主入口点。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [STAThread]
InBlock.gif        
static void Main() 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Application.Run(
new Form1());
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 根据窗体上各个控件的值创建UserInfo对象
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        private UserInfo CreateObj()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            UserInfo obj 
= new UserInfo();
InBlock.gif            obj.Name 
= this.txtName.Text;
InBlock.gif            obj.IsMan 
= this.rdoIsMan.Checked;
InBlock.gif            obj.Age 
= (int)this.numAge.Value;
InBlock.gif            obj.Birthday 
= this.dtpBirthday.Value;
InBlock.gif            
return obj;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 在窗体上显示UserInfo对象
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="obj"></param>

InBlock.gif        private void ShowObjInForm(UserInfo obj)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(obj != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.txtName.Text = obj.Name;
InBlock.gif                
this.rdoIsMan.Checked = obj.IsMan;
InBlock.gif                
this.rdoIsWoman.Checked = !obj.IsMan;
InBlock.gif                
this.numAge.Value = obj.Age;
InBlock.gif                
this.dtpBirthday.Value = obj.Birthday;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
private void btnCopyToClipboard_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            UserInfo obj 
= this.CreateObj();
InBlock.gif            
//申请注册一个新的Clipboard数据格式
InBlock.gif
            DataFormats.Format UserInfoFormat = DataFormats.GetFormat("UserInfo");
InBlock.gif            
//将对象包容到DataObj中
InBlock.gif
            IDataObject myDataObj = new DataObject(obj);
InBlock.gif            
//复制到剪贴板,并在退出程序时仍然保存
InBlock.gif
            Clipboard.SetDataObject(myDataObj,true);
InBlock.gif            MessageBox.Show(
"对象已复制到剪贴板!");
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private void btnPasteFromClipboard_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//从剪贴板中提取对象
InBlock.gif
            IDataObject retObj = Clipboard.GetDataObject();
InBlock.gif            
//判断剪贴板上的数据是否是UserInfo类型的
InBlock.gif
            if(retObj.GetDataPresent("SimpleTest.UserInfo"))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//获取UserInfo对象
InBlock.gif
                UserInfo obj = retObj.GetData("SimpleTest.UserInfo"as UserInfo;
InBlock.gif                
//在窗体上显示对象
InBlock.gif
                this.ShowObjInForm(obj);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                MessageBox.Show(
"不支持的数据格式!");
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
private void Form1_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ShowObjInForm(
new UserInfo());
ExpandedSubBlockEnd.gif        }
            
InBlock.gif        
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/raymond19840709/archive/2007/04/05/701702.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值