如何防止系统剪贴板中的图像数据被粘贴到一个WPF的RichTextBox [英] How can I prevent system clipboard image data being pasted into a WPF RichTextBox

查看:375
本文介绍了如何防止系统剪贴板中的图像数据被粘贴到一个WPF的RichTextBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码在目前的地方拦截所有剪切,复制和粘贴事件到WPF一个RichTextBox。这些都是旨在去除所有内容除外出纯文本,并允许除纯文本粘贴无(通过使用一个检查Clipboard.ContainsText()方法。)这似乎是成功地预防所有此类操作,从的的形式。用户只能复制,围绕剪切和粘贴文本,带有图像/音频数据/没有被允许随机的垃圾。

I have some code in place currently to intercept all Cut, Copy and Paste events into a RichTextBox in WPF. These are designed to strip all content out except plain text, and allow no pasting except plain text (by using a check the Clipboard.ContainsText() method.) This seems to be successful at preventing all such operations from inside the forms. A user can only copy, cut and paste text around, with images / audio data / random junk not being allowed.

不过,如果我使用PRINTSCREEN功能和粘贴入RichTextBoxes之一,图像粘贴在(不想要的行为)。如果你再尝试,虽然这个形象从一个RichTextBox中粘贴到另一个,它不会让你(所需的行为)。

However, if I use the PrintScreen function, and paste it into one of the RichTextBoxes, the image is pasted in (not the wanted behaviour.) If you then try and paste this image from one RichTextBox to another though, it won't let you (the desired behaviour).

目前,我正在使用重写

// Command handlers for Cut, Copy and Paste commands.
            // To enforce that data can be copied or pasted from the clipboard in text format only.
            CommandManager.RegisterClassCommandBinding(typeof(MyRichTextBox),
                new CommandBinding(ApplicationCommands.Copy, new ExecutedRoutedEventHandler(OnCopy), 
                new CanExecuteRoutedEventHandler(OnCanExecuteCopy)));
            CommandManager.RegisterClassCommandBinding(typeof(MyRichTextBox),
                new CommandBinding(ApplicationCommands.Paste, new ExecutedRoutedEventHandler(OnPaste), 
                new CanExecuteRoutedEventHandler(OnCanExecutePaste)));
            CommandManager.RegisterClassCommandBinding(typeof(MyRichTextBox),
                new CommandBinding(ApplicationCommands.Cut, new ExecutedRoutedEventHandler(OnCut), 
                new CanExecuteRoutedEventHandler(OnCanExecuteCut)));



OnCopy(等等),那么方法本质检查,只有文字是允许任何操作之前存在。

The OnCopy (etc) methods then essentially check that only text is present before allowing any operations.

有似乎就在这里工作的两个剪贴板,其中一个我不限制/锁定。有谁知道这个技术性问题,并在所有剪贴板活动(包括表格和系统)可以有效地锁定和定制的任何方式?

There seems to be two Clipboards at work here, one of which I'm not restricting / locking down. Does anyone know of the technicalities of this, and any way in which all Clipboard activity (both Form and System) can be locked down and customized effectively?

在此先感谢。

推荐答案

这可能是为用户有点无情,但你可以做到这一点劫持和清除粘贴前剪贴板一样简单。只需勾PreviewKeyDown(因为上的KeyUp已汇入作业插入),并清除剪贴板如果we've得到的图像,并按下Ctrl + V:

It might be a bit unforgiving for the user but you could do it as simple as hijacking and clearing the Clipboard before pasting. Just hook the PreviewKeyDown (since on KeyUp it´s already been inserted) and clear the clipboard if we´ve got an image and is pressing Ctrl+V:

public Window1()
{
    InitializeComponent();

    _rtf.PreviewKeyDown += OnClearClipboard;
}

private void OnClearClipboard(object sender, KeyEventArgs keyEventArgs)
{
    if (Clipboard.ContainsImage() && keyEventArgs.Key == Key.V && (Keyboard.Modifiers & ModifierKeys.Control) != 0)
        Clipboard.Clear();
}



不是最巧妙的解决方案,但稍后做的伎俩。

Not the neatest solution but it´ll do the trick.

这篇关于如何防止系统剪贴板中的图像数据被粘贴到一个WPF的RichTextBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆