How can I prevent system clipboard image data bein

2019-02-27 14:37发布

问题:

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.

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).

The commands I'm currently overriding are done using

// 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)));

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?

Thanks in advance.

回答1:

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.



回答2:

Actually you dont need any hack like catching the KeyDown events (which wouldn't prevent pasting through the context menu or drag and drop anyway). There's a specific attached event for that: DataObject.Pasting.

XAML:

<RichTextBox DataObject.Pasting="RichTextBox1_Pasting" ... />

Code-behind:

    private void RichTextBox1_Pasting(object sender, DataObjectPastingEventArgs e)
    {
        if (e.FormatToApply == "Bitmap")
        {
            e.CancelCommand();
        }
    }

It prevents all forms of pasting (Ctrl-V, right-click -> Paste, drag and drop).

If you want to be smarter than that, you can also replace the DataObject with one that contains only the formats you want to support (rather than completely cancelling the paste).



回答3:

I think this is probably a better way if your goal is to just allow the plain text to be pasted:

private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Control && e.KeyCode == Keys.V)
        {
            if (Clipboard.GetData("Text") != null)
                Clipboard.SetText((string)Clipboard.GetData("Text"), TextDataFormat.Text);
            else
                e.Handled = true;
        }            
    }