加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

C#的ClipBoard使用问题小结

(2010-03-29 10:26:16)
标签:

杂谈

1.使用clipboard的线程必需是STA thread,可将使用clipboard的thead的ApartmentState设为STA

cbmthread.ApartmentState = ApartmentState.STA;

 

2.剪贴板现场保护和恢复

            //保护clipboard数据
            object xx = null;
            string ctype = "";
            try
            {
                if (Clipboard.ContainsText())
                {
                    ctype = "Text";
                    xx = Clipboard.GetText();
                }
                else if (Clipboard.ContainsFileDropList())
                {
                    ctype = "FileDrop";
                    xx = Clipboard.GetFileDropList();
                }
                else if (Clipboard.ContainsImage())
                {
                    ctype = "Image";
                    xx = Clipboard.GetImage();
                }
                else if (Clipboard.ContainsAudio())
                {
                    ctype = "Audio";
                    xx = Clipboard.GetAudioStream();
                }
            }
            catch (Exception e)
            {
                xx = null;
            }

 

 

//恢复clipboard数据
            try
            {
                if (xx != null)
                {
                    if (ctype == "Text")
                    {
                        Clipboard.SetText((string)xx);
                    }
                    if (ctype == "FileDrop")
                    {
                        Clipboard.SetFileDropList((System.Collections.Specialized.StringCollection)xx);
                    }
                    if (ctype == "Image")
                    {
                        Clipboard.SetImage((System.Drawing.Image)xx);
                    }
                    if (ctype == "Audio")
                    {
                        Clipboard.SetAudio((Stream)xx);
                    }
                }
            }
            catch (Exception e)
            {
            }

 

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有