关于C#剪贴板的问题

woebird 2010-02-25 10:51:15
我想在剪贴板中同时放入对象与文本信息,以便其它程序读取
实现方案如下:
自定义类:
[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],则获取不是剪贴板中的对象。如果自定义的类继承于系统的类,这个问题应该怎么解决?
...全文
209 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
woebird 2010-02-25
  • 打赏
  • 举报
回复
难道是我问题没有描述清楚?
woebird 2010-02-25
  • 打赏
  • 举报
回复
csdn上的专家那去了?
woebird 2010-02-25
  • 打赏
  • 举报
回复
谢谢参与,剪贴 板的原理我不清楚,希望大家能指条明路!
冈仁 2010-02-25
  • 打赏
  • 举报
回复
不老会的 看看 ~~~~~~~~
nature025 2010-02-25
  • 打赏
  • 举报
回复
好深奥,帮顶~~~~~~~~~~~~
xiezechang 2010-02-25
  • 打赏
  • 举报
回复
好深奥,帮顶~~~~~~~~~~~~

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧