关于C#剪贴板的问题
我想在剪贴板中同时放入对象与文本信息,以便其它程序读取
实现方案如下:
自定义类:
[Serializable]
public class CustomTypeP
{
public bool flag = false;
}
[Serializable]
public class CustomType : CustomTypeP
{
public int i = 0 ;
public string name = "CustomType";
}
在剪贴板中注册自定义类型
[DllImport("User32.dll",CharSet=CharSet.Auto,SetLastError = true)]
public extern static void RegisterClipboardFormat(string formatter);
RegisterClipboardFormat(Type.GetType("Sample.CustomType").ToString());
RegisterClipboardFormat(Type.GetType("Sample.CustomTypeP").ToString());
在剪帖设置对象及信息
IDataObject idata = new DataObject();
idata.SetData(DataFormats.Text, copyContent.ToString());
idata.SetData(Type.GetType("Sample.CustomType").ToString(), new CustomType());
Clipboard.SetDataObject(idata, true);
在本程序中能以下方式读取剪贴 板中的文本与对象:
if(Clipboard.ContainsData(Type.GetType("Sample.CustomType").ToString()))
{
CustomType customType = Clipboard.GetData(Type.GetType("Sample.CustomType").ToString()) as CustomType;
string pasteString = Clipboard.GetText();
}
到此文本与对象都能获得,但是如下自定的父类没有声名序列化即 [Serializable],则获取不是剪贴板中的对象。如果自定义的类继承于系统的类,这个问题应该怎么解决?
实现方案如下:
自定义类:
[Serializable]
public class CustomTypeP
{
public bool flag = false;
}
[Serializable]
public class CustomType : CustomTypeP
{
public int i = 0 ;
public string name = "CustomType";
}
在剪贴板中注册自定义类型
[DllImport("User32.dll",CharSet=CharSet.Auto,SetLastError = true)]
public extern static void RegisterClipboardFormat(string formatter);
RegisterClipboardFormat(Type.GetType("Sample.CustomType").ToString());
RegisterClipboardFormat(Type.GetType("Sample.CustomTypeP").ToString());
在剪帖设置对象及信息
IDataObject idata = new DataObject();
idata.SetData(DataFormats.Text, copyContent.ToString());
idata.SetData(Type.GetType("Sample.CustomType").ToString(), new CustomType());
Clipboard.SetDataObject(idata, true);
在本程序中能以下方式读取剪贴 板中的文本与对象:
if(Clipboard.ContainsData(Type.GetType("Sample.CustomType").ToString()))
{
CustomType customType = Clipboard.GetData(Type.GetType("Sample.CustomType").ToString()) as CustomType;
string pasteString = Clipboard.GetText();
}
到此文本与对象都能获得,但是如下自定的父类没有声名序列化即 [Serializable],则获取不是剪贴板中的对象。如果自定义的类继承于系统的类,这个问题应该怎么解决?
...全文
相关推荐
WinClipboardApi:只是另一个剪贴板winapi包装器(可以从Windows服务向剪贴板提供访问权限)
WinClipboardApi 只是另一个剪贴板winapi包装器(可以从Windows服务向剪贴板提供访问权限)
C# 剪贴板内容修改器
C# 剪贴板内容修改器,可以直接过滤修改或替换剪贴板内容中特定字符
C#监控剪贴板
在C#监控剪贴板,只需要复杂,则程序中就能自动获取剪贴板中的内容
c#写的剪贴板clipboard源代码
这是我用c#写的剪贴板clipboard源代码,写得很简单,有待改善 ----SZZ NUPT
从剪贴板提取EMF图元文件并保存的C#工程,绝对有用!
从剪贴板提取EMF图元文件并保存的C#工程,绝对有用!


