WPF 复制粘贴到系统粘贴板中以供鼠标右键粘贴

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_23018459/article/details/79297733
        
/// <summary>
        /// 复制或剪切文件到剪切板
        /// </summary>
        /// <param name="files">文件路径数组</param>
        /// <param name="cut">true:剪切;false:复制</param>
        public static void CopyToClipboard(string[] files, bool cut)
        {
            if (files == null) return;
            IDataObject data = new DataObject(DataFormats.FileDrop, files);
            MemoryStream memo = new MemoryStream(4);
            byte[] bytes = new byte[] { (byte)(cut?2:5),0,0,0};
            memo.Write(bytes,0,bytes.Length);
            data.SetData("PreferredDropEffect",memo);
            Clipboard.SetDataObject(data,false);
        }

      
        主要调用Clipboard中的方法。复制文本调用如下
  private void La_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            StackPanel st = e.Source as StackPanel;
            StringBuilder Copystr = new StringBuilder();
            foreach (TextBox tb in GetChildObjects<TextBox>(st))
            {
                Copystr.Append(" ");
                Copystr.Append(tb.Text);
                SolidColorBrush myBrush = new SolidColorBrush(System.Windows.Media.Color.FromArgb(0xFF, 0xad, 0xd6, 0xff));
                tb.Background = myBrush;
            }
            Clipboard.SetText(Copystr.ToString());
            MessageToolTip.Show("数据复制成功");
        }

阅读更多
换一批

没有更多推荐了,返回首页