Clipboard is a Clipboard. When we press Ctrl + C or click Copy in the menu, the selected object is stored in the Clipboard. If the selected object is a file, press Ctrl + V or click "Paste" after the drive letter. Then, the selected file is saved to the specified disk; if the selected object is an image, press Ctrl + V or click "Paste" to display the image, after opening the text editor, press Ctrl + V or click "Paste" to display the text in the text editor. Of course there are many types of selected objects, so we will not give them one by one. These operations are typical operations of the clipboard. So what exactly are these operations implemented using Visual C? This article will discuss this issue through a program.
I. software environment for program design and operation in this article
(1). Microsoft Windows 2000 Server Edition
(2). Net FrameWork SDK Beta 2
Ii. Main functions and running interface of the program
In fact, after reading the above introduction, it is not difficult to obtain several basic operations of the clipboard, that is, copying data to the scissors
Slice and extract the data from the current clipboard. Follow these steps to compile a program. The main form of this program is composed of two TextBox components: TextBox1 and TextBox2, and two Button components. The function is to copy data to the clipboard by pressing the copy specified data to the clipboard after the TextBox1 text is selected, then press the "get data from current Clipboard" button to obtain the data in the clipboard and display it in TextBox2. The following figure shows the program running interface:
Figure 1 copy a specified object to the clipboard
Figure 2 obtain data from the current clipboard and display it
3. program design ideas and solutions to important steps
(1). How to copy a specified object to the clipboard:
In this step, we need to deal with two problems: first, how to determine whether the objects in TextBox1 are selected, and second, how to store the selected objects in the clipboard. The following is an analysis of these two problems.
I>. How to determine whether an object is selected in TextBox1:
The TextBox component has a SelectedText attribute, which is the selected text. In the program, it judges whether the property value is "" to determine whether the selected object is in TextBox1.
II>. How to store the selected objects in the clipboard:
To complete this function, you need to use the Clipboard class in. The Clipboard class is a closed class, which determines that it cannot be inherited. The main function of this class is to access the data in the Clipboard. The SetDataObject () method stores data in the clipboard.
The code for solving the above two problems is as follows:
If (textBox1.SelectedText! = "")
Clipboard. SetDataObject (textBox1.SelectedText );
Else
TextBox2.Text = "No selected content in textBox1 ";
(2) read and display the data in the clipboard.
To implement this step, you also need to solve two problems: first, you can not only store text, but also store other types of data in the clipboard, textBox2 can display only text data. Therefore, to ensure better program stability, you must determine whether the data type in the clipboard is text before display. Second, read the data in the clipboard, and displayed.
I>. Determine the data type in the current clipboard:
To determine the data type in the clipboard, use the IdataObject interface, which can store the data read from the clipboard. Use the IdataObject interface's GetDataPresent () method to determine whether the data type meets your needs.
II>. Read the data in the clipboard and display it:
You can use the GetDataObject () method of the Clipboard class to store data in the current Clipboard to the IdataObject interface. However, the data in the IdataObject interface cannot be directly used by the program. You must use the GetData () method of the IdataObject interface () obtain data. However, because the return value of GetData () is an Object type variable, that is, the reference type variable, and the text displayed in TextBox1 is a real-value type variable, an export operation is required, this operation is described in detail in the previous article. After the export operation, the data can be used by TextBox1. The following code solves the above two problems:
IDataObject iData = Clipboard. GetDataObject ();
// Check whether the data is in the available format, that is, the text format.
If (iData. GetDataPresent (DataFormats. Text ))
{
// It can be used to display the content in the clipboard in textbox2.
TextBox2.Text = (String) iData. GetData (DataFormats. Text );
}
Else
{
// If not
TextBox2.Text = "No data is received from the clipboard! ";
}
4. source code in this article
After mastering the main steps of the program, it is not difficult to get the following program code:
Using System;
Using System. Drawing;
Using System. Collections;
Using System. ComponentModel;
Using System. Windows. Forms;
Using System. Data;
Public class Form1: Form
{
Private Button button1;
Private Button button2;
Private TextBox textBox1;
Private TextBox textBox2;
Private System. ComponentModel. Container components = null;
Public Form1 ()
{
InitializeComponent ();
}
Protected override void Dispose (bool disposing)
{
If (disposing)
{
If (components! = Null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}
Private void InitializeComponent ()
{
This. button1 = new Button ();
This. button2 = new Button ();
This. textBox1 = new TextBox ();
This. textBox2 = new TextBox ();
This. SuspendLayout ();
This. button1.Location = new System. Drawing. Point (264, 56 );
This. button1.Size = new System. Drawing. Size (96, 40 );
This. button1.Name = "button1 ";
This. button1.TabIndex = 0;
This. button1.Text = "copying the specified data to the Clipboard ";
This. button1.Click + = new System. EventHandler (this. button#click );
This. button2.Location = new System. Drawing. Point (264,104 );
This. button2.Size = new System. Drawing. Size (96, 40 );
This. button2.Name = "button2 ";
This. button2.TabIndex = 1;
This. button2.Text = "getting data from the current Clipboard ";
This. button2.Click + = new System. EventHandler (this. button2_Click );
This. textBox1.Location = new System. Drawing. Point (64, 64 );
This. textBox1.Name = "textBox1 ";
This. textBox1.TabIndex = 2;
This. textBox1.Text = "";
This. textBox2.Location = new System. Drawing. Point (64,104 );
This. textBox2.Name = "textBox2 ";
This. textBox2.TabIndex = 3;
This. textBox2.Text = "";
This. AutoScaleBaseSize = new System. Drawing. Size (6, 14 );
This. ClientSize = new System. Drawing. Size (384,273 );
This. Controls. Add (textBox2 );
This. Controls. Add (textBox1 );
This. Controls. Add (button2 );
This. Controls. Add (button1 );
This. Name = "Form1 ";
This. Text = "Visual C # To operate Clipboard! ";
This. ResumeLayout (false );
}
Static void Main ()
{
Application. Run (new Form1 ());
}
Private void button#click (object sender, System. EventArgs e)
{
If (textBox1.SelectedText! = "")
Clipboard. SetDataObject (textBox1.SelectedText );
Else
TextBox2.Text = "No selected content in textBox1 ";
}
Private void button2_Click (object sender, System. EventArgs e)
{
IDataObject iData = Clipboard. GetDataObject ();
// Check whether the data is in the available format, that is, the text format.
If (iData. GetDataPresent (DataFormats. Text ))
{
// It can be used to display the content in the clipboard in textbox2.
TextBox2.Text = (String) iData. GetData (DataFormats. Text );
}
Else
{
// If not
TextBox2.Text = "No data is received from the clipboard! ";
}
}
}
V. Summary
So far, we have completed all the work of using Visual C # to implement basic operations on the clipboard. This is to copy data to the clipboard, read the data needed in the clipboard, and display the data in the clipboard. The content of the previous two aspects is the most important. Although the above operations and processing targets all the objects in text, they are actually the same for other types of objects. You only need to make a few modifications. Do not believe it.