SoFunction
Updated on 2025-03-07

C# uses the Clipboard class to implement clipboard function

Clipboard is one of the most commonly used features in Windows operating systems. It is used to pass data from one application to another, which can be text, images, or even program objects.

However, the clipboard also has restrictions. It can only point to one piece of content at a certain time, and each subsequently copied content will replace the previous content.

In order to operate the clipboard (including reading and writing content) in C#, you need to use the class

1. Store things into the clipboard. Equivalent to Ctrl+C

The SetDataObject(Object obj,bool copy) method of the Clipboard class is mainly used. This method is to put the target object into the clipboard.

obj is the target object.

The parameter copy indicates whether the object is still saved on the clipboard at the end of the program. If set to false, or not, the clipboard contents will not be retained after the program runs.

1. Store ordinary data

();

The copy operation is completed through the above code. You can open the text document and press Ctrl+V to see the effect.

2. Receive custom data into the clipboard

The data structure of C# objects cannot be understood by the clipboard, so if you want to put your customized data on the clipboard, you must serialize it. In actual operation, you must provide a "serializable" object.
Notice:

  • Objects must be serializable (Serializable annotation) before they can be placed on the clipboard. If you pass a non-serialized object to a clipboard method, the method will fail and no exception is thrown.
  • The Clipboard class can only be used in threads that are set to single-threaded unit (STA) mode. To use this, make sureMainMethod UseSTAThreadAttributeAttributes are marked.
  • If the data is more complicated, you can consider serializing the data to a Stream object by itself, and then writing the Stream object to the clipboard. When obtaining it, deserializing the Stream object by itself and restoring the data.

Here is a simple example:

[Serializable]
public class User {
    public int age { get; set; }
    public string name { get; set; }
}

class Program {
    [STAThread]
    static void Main(string[] args) {
        User userIn = new User();
         = "Jack";
         = 18;
        ("mydata", userIn);
        User userOut = (User)("mydata");
        ( +" | " + );
    }
}

The last thing to note is that since the data type here is "mydata", you can also specify another name. Only your own program can understand this type of data. In other words, you cannot open Notepad or Photoshop and directly paste your User object.

2. Read things from the clipboard. Equivalent to Ctrl+V

The main method used is: GetDataObject() is used to retrieve data in the clipboard. It returns data in any format and returns the IDataObject interface to receive any data.

  • The GetDataPresent (Type Format) method of the IDataObject interface instance returns the bool value to get whether the specified type is available.
  • Another is the DataFormats class, which is mainly used to identify formats.
  • Obtaining data from the clipboard does not affect the contents of the clipboard, and the return value of GetData is a copy stored on the clipboard.

1. Methods to paste text:

IDataObject iData = ();
if (())
{
    //If the data in the clipboard is in text format    this. = (string)();//Retrieve data associated with the specified format}
else
{
    ("Currently, data in the clipboard cannot be converted into text", "mistake");
}

2. Paste the picture:

if (())
 {
     Image img = ();
      = img;
 }

or

IDataObject iData = ();
 if (())
 {
     this. = (Bitmap)();
 }
 else
 {
     ("Currently, data in the clipboard cannot be converted into pictures", "mistake");
 }

3. The paste method that comes with the control

In addition, we can use the paste method provided by some controls to paste. Let's take richtextbox as an example here. [Note: Before using the control's paste method, you must ensure that the control's readonly property is false, otherwise you cannot do this on the control! ]

Now we use the paste method to add images to richtextbox.

OpenFileDialog fd = new OpenFileDialog();
 = "Picture Files|*.jpg|All Files|*.*";
if (() == )
{
    ((), true);
     myFormat = ();
     = false;
    if ((myFormat))//Judge whether this control can paste data in this format    {
        (myFormat);
    }
    else
    {
        ("Cannot paste!");
    }
}

This is all about this article about C# using the Clipboard class to implement clipboard functions. I hope it will be helpful to everyone's learning and I hope everyone will support me more.