[Paste] C # operating system clipboard

Source: Internet
Author: User
C # operating system clipboard

Clipboard is one of the most common functions in windows. It is used to transmit data from one application to another. It can be text, images, or even program objects. However, there are limits on the clipboard. It can only point to one piece of content at a specific time, and each copied content will replace the previous content. To operate the clipboard (including reading and writing content) in C #, you must use system. windows. forms. clipboard class. Next we will illustrate how to write data to the clipboard with an example. Let's take a look at the running effect before writing the program:

As shown in, you can enter some text in the text box and click Copy button. Then the program writes the data in the text box to the clipboard, and then press Ctrl + V in notepad, did you see the data you just entered in the text box appear in notepad! The implementation code is as follows:
Private void button#click (Object sender, system. eventargs E)
{
// Exit the button
Close ();
}
// Copy the button
Private void button2_click (Object sender, system. eventargs E)
{
If (this. textbox1.text = "")
{
MessageBox. Show ("data must be input before copy", "error ");
Return;
}
Else
// The setdataobject (Object OBJ, bool copy) method places data on the clipboard
// The OBJ parameter indicates the data object to be placed.
// The copy parameter indicates whether the data is still saved on the clipboard when the program exits.
Clipboard. setdataobject (this. textbox1.text, true );
}
The above program explains how to write data to the clipboard. Next we will look at how to read the content in the clipboard in the program, so we can look at the running effect of the program:

Program components:
Create a Windows program and place three buttons, one label, and one picturebox on the form.

Program operation:
1. Copy a short text section somewhere and click the "display clipboard text" button in the program. Then, the text you copied will appear in the label of the form.
2. open a webpage and Right-click a set of images and choose save from the shortcut menu. Then, return to the program and click the "show pictures in the Clipboard" button, the image you copied will appear in the picturebox of the form.

Some classes or methods used by the Program:
The idataobject interface provides a format-independent mechanism for data. That is to say, the data that can be stored using this object is not restricted by the format, because we do not know in advance what format the data in the clipboard is.

Use the getdataobject () method of the clipboard class to obtain the data in the clipboard. This method returns an idataobject

Use the getdatapresent (system. type format) determines whether the data stored in the idataobject object can be converted to a specified format. This method receives a parameter. This parameter must be a predefined format type, this method returns the bool value.

Finally, use the getdata (system. Type format) method of the idataobject object to obtain the data content. This method returns the object type conversion before use.
The Code is as follows:
Private void button#click (Object sender, system. eventargs E)
{
// Getdataobject
Idataobject idata = clipboard. getdataobject ();

// Match the data with the specified format and return bool
If (idata. getdatapresent (dataformats. Text ))
{
// Getdata retrieves data and specifies a format
This. label1.text = (string) idata. getdata (dataformats. Text );
}
Else
{
MessageBox. Show ("Currently, data in the clipboard cannot be converted to text", "error ");
}
}

Private void button2_click (Object sender, system. eventargs E)
{
Idataobject idata = clipboard. getdataobject ();

If (idata. getdatapresent (dataformats. Bitmap ))
{
This. picturebox1.sizemode = pictureboxsizemode. stretchimage;
This. picturebox1.image = (Bitmap) idata. getdata (dataformats. Bitmap );
}
Else
{
MessageBox. Show ("Currently, the data in the clipboard cannot be converted to an image", "error ");
}
}

Private void button3_click (Object sender, system. eventargs E)
{
Close ();
}

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.