0

I am using a winform HTML Editor which uses AxSHDocVw.AxWebBrowser. User are copying and pasting text from other software into this control. The problem is that, on paste Ctrl-V it add few font tags to preserve formatting. I do not want to preserve formatting, it should paste clean text without formatting or at least should not add there FONT tags. What i think is to intercept Ctrl-V and before pasting cleanup clipboard text.

So, I tried to intercept WM_PASTE message and replace clipboard content with fixed test (just to check) as below

class myWB : AxSHDocVw.AxWebBrowser
{
    protected override void WndProc(ref Message m)
    {
        if (m.Msg == 0x302)     // Trap WM_PASTE:
        {
            Clipboard.Clear();
            Clipboard.SetText("some text");
            return;
        }
        base.WndProc(ref m);
    }
}

But it was not working. I added following before IF block to see if it is receiving WM_PASTE message.

Debug.WriteLine(m.Msg);

On run, i didn't see there 0x302 (770) in output window even after multiple Ctrl-V.

Is it not receiving that message?

Then what is the way to do it? How do i cleanup text before paste?

Jens Björnhager
  • 5,632
  • 3
  • 27
  • 47
SamTech
  • 1,305
  • 2
  • 12
  • 22

1 Answers1

2

WM_PASTE is not a notification, it is a command. That you'd send to an EDIT control to have it paste the clipboard into the control.

Of course a web browser is not an edit box so doesn't do it the same way. You will need to intercept the IHtmlElement2.onpaste event instead.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Hi, thanks for answering. Can you please provide some reference/link where i could find some help on how to handle `IHtmlElement2.onpaste` event for `AxWebbrowser`. – SamTech Jan 12 '13 at 14:08
  • Just type "onpaste event" in a google query. All the top links look relevant. – Hans Passant Jan 12 '13 at 14:12
  • Ok i created `IHtmlElement2` as `IHTMLDocument2 doc = (IHTMLDocument2)editorWebBrowser.Document;` but there is no onpaste event? for doc object ? – SamTech Jan 12 '13 at 14:27
  • You create an IHTMLDocument2. Get an IHtmlElement2 from the Body property. – Hans Passant Jan 12 '13 at 14:28
  • I took `HtmlEventProxy` class from [here](http://www.codeproject.com/Articles/25769/Handling-HTML-Events-from-NET-using-C) and attached `onpaste` event for body `IHtmlElement2` but still it is not working. Due to lack of time i applied a dirty trick. I used Activate event of form and cleaning up clipboard text. i know is not a good thing but right now i do not have time to dig into issue. May be see later. – SamTech Jan 13 '13 at 04:41