Use clipboard to save custom objects

Source: Internet
Author: User

Technical points:
Objects placed on the clipboard must be serializable.
The clipboard receives an object that implements the IDataObject interface. You can use this object to "Wrap" a specific data object.
You can call the IDataObject. SetData method multiple times to copy multiple types of objects to the clipboard. Use DataFormat to identify the data in the clipboard.
Before pasting, make sure that the data on the clipboard is in the required format.



 

Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Drawing;
Using System. Runtime. Serialization;
 
Namespace UseClipboard
{
[Serializable]
Class MyPic
{
Public Image pic; // Image
Public string picInfo; // Image Description
}
}
 
 
Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Text;
Using System. Windows. Forms;
 
Namespace UseClipboard
{
Public partial class frmMain: Form
{
Public frmMain ()
{
InitializeComponent ();
}
// Image
Private Image bmp
{
Get
{
Return pictureBox1.Image;
}
Set
{
PictureBox1.Image = value;
}
}
// Image Description
Private string info
{
Get
{
Return txtImageInfo. Text;
}
Set
{
TxtImageInfo. Text = value;
}
}
 
Private void btnLoadPic_Click (object sender, EventArgs e)
{
ChooseImageFile ();
}
 
// Select the image www.2cto.com
Private void ChooseImageFile ()
{
If (openFileDialog1.ShowDialog () = DialogResult. OK)
{
String name = openFileDialog1.FileName;
TxtImageInfo. Text = name;
Bmp = new Bitmap (name );
}
}
// Create an object based on user-defined information
Private MyPic CreateMyPicObj ()
{
MyPic obj = new MyPic ();
Obj. pic = bmp;
Obj. picInfo = info;
Return obj;
}
 
// Copy the object to the clipboard
Private void CopyToClipboard ()
{
// Create a MyPic object
MyPic obj = CreateMyPicObj ();
 
// Create a data object and load the object of the MyPic type
IDataObject dataobj = new DataObject (obj );
 
// Other types of data can also be loaded into the data object.
Dataobj. SetData (DataFormats. UnicodeText, info );
Dataobj. SetData (DataFormats. Bitmap, bmp );
 
// Copy to the clipboard. The second parameter indicates that the clipboard is not cleared when the program exits.
Clipboard. SetDataObject (dataobj, true );
}
 
Private void btnExit_Click (object sender, EventArgs e)
{
Close ();
}
 
Private void btnCopyToClipboard_Click (object sender, EventArgs e)
{
CopyToClipboard ();
}
 
// Obtain data from the clipboard
Private void PasteFromClipboard ()
{
// Do I have the data I need on the clipboard? The format is "project name. Data Format name"
If (Clipboard. ContainsData ("UseClipboard. MyPic") = false) // retrieves data objects based on the specified DataFormat
Return;
// Read data
IDataObject clipobj = Clipboard. GetDataObject ();
// Convert data to the required type
MyPic mypicobj = clipobj. GetData ("UseClipboard. MyPic") as MyPic;
// Extract the required data from the data object
Info = mypicobj. picInfo;
PictureBox1.Image = mypicobj. pic;
 
If (Clipboard. ContainsData (DataFormats. UnicodeText) = false) // retrieves data objects based on the specified DataFormat
Return;
String str = clipobj. GetData (DataFormats. UnicodeText) as string;
MessageBox. Show (str );
}
 
Private void btnPasteFromClipboard_Click (object sender, EventArgs e)
{
PasteFromClipboard ();
}
}
}

 


From the conong age

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.