简体   繁体   English

保存任何剪贴板内容并重新加载?

[英]Save any Clipboard content and reload it?

I need to use clipboard memory for 1 sec, doing some stuff, and reload the clipboard as original.我需要使用剪贴板 memory 1 秒,做一些事情,然后重新加载原始剪贴板。

I want my cliboard do:我想让我的剪贴板做:

  • "original random text, image, sound or anything else" --> "My Useful Text" “原始随机文本、图像、声音或其他任何内容” --> “我的有用文本”
  • "My Useful Text" --> "random text, image, sound or anything else" “我的有用文本”-->“随机文本、图像、声音或其他任何内容”

But what my clipboard do:但是我的剪贴板做了什么:

  • "original random text, image, sound or anything else" --> "My Useful Text" “原始随机文本、图像、声音或其他任何内容” --> “我的有用文本”
  • "My Useful Text" --> " " [nothing] “我的有用文字”-->“”[无]
using System.Windows.Forms;

public class ExplorerTools 
{
  public void Run()
  {
    // saving the current clipboard
    var oldClipboard = Clipboard.GetDataObject();

    // Doing some stuff with the clipboard
    Clipboard.SetText("My Useful Text");
    System.Threading.Thread.Sleep(400);

    // I want to put back oldClipboard in the clipboard
    Clipboard.SetDataObject(oldClipboard);
  }
}

I don't understand why Clipboard.SetDataObject() cannot retrieve the old value, he just put nothing ='(我不明白为什么Clipboard.SetDataObject()无法检索旧值,他什么也没放 ='(

Like @Taw said, i can't directly use the IDataObject i get from Clipboard.GetDataObject() .正如@Taw 所说,我不能直接使用从Clipboard.GetDataObject()获得的 IDataObject。 So I have to save every 21 kind of clipboard data and reload it individually myself.所以我必须保存每 21 种剪贴板数据并自己单独重新加载。

using System.Windows.Forms;

public class ExplorerTools 
{
  public void Run()
  {
    // saving the current clipboard
    string[] arr_format = Clipboard.GetDataObject().GetFormats(false);
    Dictionary<string, object> dic_oldDataClipboard = new Dictionary<string, object>();
    foreach(string format in arr_format)
      dic_oldDataClipboard.Add(format, Clipboard.GetData(format));


    // Doing some stuff with the clipboard
    Clipboard.SetText("My Useful Text");
    System.Threading.Thread.Sleep(400);


    // I want to put back oldClipboard in the clipboard
    foreach(var format_data in dic_oldDataClipboard)
      Clipboard.SetData(format_data.Key, format_data.Value);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM