兔八哥极品软件园    运行: 4507天 | 文章:640 篇 | 评论:527 条 | 碎语:1条

C#获取和设置剪切板数据

作者:admin 发布于:2012-8-25 13:54 Saturday 分类:.NET


//向操作系统剪切板设置文本数据
        public static bool SetTextToClipboard(string strText)
        {
            if (strText != null && strText.Length > 0)
            {
                try
                {
                    System.Windows.Forms.DataObject dataObject = new System.Windows.Forms.DataObject();
                    dataObject.SetData(System.Windows.Forms.DataFormats.UnicodeText, true, strText);
                    System.Windows.Forms.Clipboard.SetDataObject(dataObject, true);
                    return true;
                }
                catch
                {

                }
            }
            return false;
        }

        //从操作系统剪切板获得文本
        public static string GetTextFromClipboard()
        {
            try
            {
                System.Windows.Forms.IDataObject data = System.Windows.Forms.Clipboard.GetDataObject();
                if (data.GetDataPresent(System.Windows.Forms.DataFormats.UnicodeText))
                {
                    string strText = (string)data.GetData(System.Windows.Forms.DataFormats.UnicodeText);
                    return strText;
                }
            }
            catch
            { }
            return null;
        }

标签: 剪切板


Powered by 兔八哥极品软件 苏ICP备12049267号 sitemap