C # operating system clipboard
The clipboard is one of the most common functions in the Windows operating system, which is used to pass data from one application to another, which can be text, images, and even program objects. However, the clipboard also has restrictions, which can only point to a piece of content at a certain time, and each subsequent copying content will replace the previous content. In order to operate the clipboard (including reading and writing) in the C #, you need to use the System.Windows.Forms.clipboard class. Let's first describe how to write data to the clipboard in an example. Let's take a look at the operation: As shown above, you can enter some text in the text box then click to copy the button, then the program writes the data in the text box to the clipboard, then open the notepad Lower Ctrl V key, is it to see the data that has just been entered in the text box? The code is as follows: private void button1_click (object sender, system.eventargs e) {// exit button close ();} // copy button, private void button2_click (object sender, system.eventargs e) {if (THIS.TEXTBOX1 .Text == "") {MessageBox.show ("Copy must be input before"); return;} Else // setDataObject (Object Obj, Bool Copy) Method to place the data on the clipboard // parameter Obj refers to data object // parameter COPY means that the data is still saved on the clipboard at clipboard.setDataObject (THISTEXTBOX1.TEXT, TRUE);} How to write data in the clipboard when the program exits. Next, let's take a look at how to read the contents in the clipboard in the program, so let's take a look at the operation of the program: Program Components: New Windows programs, put 3 Button and 1 Label and 1 on the form Picturebox.
Program operation: 1. Copy a small text in somewhere, then return to the program Click "The text in the clipboard" button, then the text you just copied will appear in the form of Label 2. Open a web page Click Right click on a palate to select Save, then return to the program "Show the picture in the clipboard" button, then the picture you just copied will appear in the form of PictureBox Some classes or methods used by the program: iDataObject interface, mechanism-free mechanisms for data and format, that is, the data that can be stored using this object is unfained because we don't know what the data in the clipboard is in advance. The getDataObject () method using the ClipBoard class gets the data in the clipboard. This method returns an iDataObject to determine whether the data stored in the iDataObject object can be converted to a specified format if the data of the IDataObject object can be converted to the specified format. A parameter This parameter must be a format type of system predefined, which returns the BOOL value to get the data content using the iDataObject object's getData (System.Type format) method, which returns to Object to use the type conversion specific code as follows : Private void button1_click (object sender, system.eventargs e) {// getDataObject Retrieves data iDataObject iData = clipboard.getdataObject (); // with the specified format, return BOOL if (iData.) GetDataPresent (dataFormats.text) {// getData retrieves data and specifies a format this.label1.text = (string) iData.Text);} else {messagebox.show ("Data in the current clipboard is not convertible For 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 ( "current Clip The data is not converted to the picture "," error ");}}}} private void button3_click (object sender, system.eventargs e) {close ();