C#备份和恢复剪贴板 [英] C# Backing Up And Restoring Clipboard

查看:314
本文介绍了C#备份和恢复剪贴板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用剪贴板程序,但我想剪贴板恢复到以前的状态,我用它做后

I have a program that uses clipboard but I want to restore the clipboard to its former state after I am done with it.

这是我的代码:

IDataObject temp = Clipboard.GetDataObject();

//Some stuff that change Cliboard here
Clipboard.SetText("Hello");
//Some stuff that change Cliboard here

Clipboard.SetDataObject(temp);



但是,如果我复制文本,并运行此代码,我得到记事本什么。

But it if I copy a text, and run this code, I get nothing on notepad.

请注意:因为我想保留EXACLY剪贴板是怎么回事之前,即使用户复制的文件,我不能使用Clipboard.Contains

NOTE : I can't use Clipboard.Contains because I want to preserve the Clipboard EXACLY how it was before, even if the user copied a file.

推荐答案

我不能肯定这是否会工作,但我看不出有什么理由你不应该能够备份使用的实际较长的做法数据,读取数据,之后恢复它

I cannot confirm whether this will work, but I see no reason why you shouldn't be able to back up the data using the longer approach of actually reading the data and restoring it afterwards.

在这里阅读:的 http://msdn.microsoft.com/en-us/library/system.windows.forms.idataobject.aspx

您会做类似的信息(伪代码)

You would do something like (pseudo-code)

//Backup
var lBackup = new Dictionary<string, object>();
var lDataObject = Clipboard.GetDataObject();
var lFormats = lDataObject.GetFormats(false);
foreach(var lFormat in lFormats)
{
  lBackup.Add(lFormat, lDataObject.GetData(lFormat, false);
}

//Set test data
Clipboard.SetText("asd");

//Would be interesting to check the contents of lDataObject here

//Restore data
foreach(var lFormat in lFormats)
{
  lDataObject.SetData(lBackup[lFormat]);
}
//This might be unnecessary
Clipboard.SetDataObject(lDataObject);

这篇关于C#备份和恢复剪贴板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆