tags:

views:

28

answers:

1

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  A: 

Hi!

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.

Almund
Possibly a stupid question, but would this only affect the key-presses for the program, or would it also affect all presses of those key combinations for the rest of the system? I just don't want to disrupt the user's working on the rest of the system by accident! :)
Smallgods
Experimented with this approach, and it works perfectly! I hadn't thought of approaching it from this angle, so thankyou :) Left my approach in place, as it helps to format the text as I want it, but your suggestion fixes the final issue I was having. Cheers again!
Smallgods
Glad to help! It will affect the system in such a way that if you try to paste the print-screened image in the rtf it will be lost but otherwise, as long as you only trap previewkeydown on the rtf it _should_ only do this as longas your RTF box is focused. If you´re window isn't focused at all I guarantee it won't trap the event.
Almund