WPF 剪切板操作

<Window x:Class="WpfApplication1.IDataObjectWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="IDataObjectWindow" Height="300" Width="300">
    <StackPanel>
        <TextBox Name="tbx" AcceptsReturn="True" VerticalScrollBarVisibility="Visible" Height="100" />
        <Button Click="Button_Click" Content="复制并粘贴" />
        <TextBlock Text="下面是剪切板内信息" />
        <StackPanel Name="view" />
    </StackPanel>
</Window>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace WpfApplication1
{
    /// <summary>
    /// IDataObjectWindow.xaml 的交互逻辑
    /// </summary>
    public partial class IDataObjectWindow : Window
    {
        public IDataObjectWindow()
        {
            InitializeComponent();
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var strs = tbx.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
                var s = strs[0];
                var i = Convert.ToInt32(strs[1]);
                var img = new BitmapImage(new Uri(strs[2]));
                var data = new DataObject();
                data.SetData(DataFormats.UnicodeText, s);
                data.SetData(typeof(int), i);
                data.SetData(DataFormats.Bitmap, img);
                Clipboard.SetDataObject(data, true);
                Paste();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        void Paste()
        {
            view.Children.Clear();
            view.Children.Add(new TextBlock { Text = "文字:" + Clipboard.GetText() });
            view.Children.Add(new TextBlock { Text = "数字:" + Clipboard.GetData(typeof(int).FullName).ToString() });
            view.Children.Add(new Image { Source = Clipboard.GetImage() });
        }
    }
}


知识共享许可协议
《WPF 剪切板操作》常伟华 创作。
采用 知识共享 署名-相同方式共享 3.0 中国大陆 许可协议进行许可。
  • 多说评论
  • 签名
  • 新浪微博
  • 默认评论
  • Tab Header 5

0 条评论 / 点击此处发表评论

Tab Content 5

开发技术


开发平台和工具

sitemap     155.65ms