开发者

Intercept paste event on HtmlEditor WinForms

I'm using a HtmlEditor control inside a Windows Form.

I got the control from this page:

http://windowsclient.net/articles/htmleditor.aspx

I want to extend the controls functionality by allowing the user to paste images from the clipboard. Right now you can paste plain and formatted text, but when trying to paste an image it does no开发者_如何学JAVAthing.

Basically what I thought was to detect when the user presses Ctrl+V on the editor, check the clipboard for images and if there's an image, insert it manually to the editor.

The problem with this approach is that I cannot get the OnKeyDown or OnKeyPress events of the form to be raised.

I have the KeyPreview property set to true on the form, but still the events aren't raised.

I also tried to Subclass the form and the editor (as explained here) to intercept the WM_PASTE message, but it isn't raised either.

Any ideas on how to achieve this?

Thanks a lot


I spent all day on this problem and finally have a solution. Trying to listen for the WM_PASTE message doesn't work because Ctrl-V is being PreProcessed by the underlying mshtml Control. You can listen for OnKeyDown/Up etc to catch a Ctrl-V but this won't stop the underlying Control from proceeding with its default Paste behavior. My solution is to prevent the PreProcessing of the Ctrl-V message and then implementing my own Paste behavior. To stop the control from PreProcessing the CtrlV message I had to subclass my Control which is AxWebBrowser,

public class DisabledPasteWebBrowser : AxWebBrowser
{
    const int WM_KEYDOWN = 0x100;
    const int CTRL_WPARAM = 0x11;
    const int VKEY_WPARAM = 0x56;

    Message prevMsg;
    public override bool PreProcessMessage(ref Message msg)
    {
        if (prevMsg.Msg == WM_KEYDOWN && prevMsg.WParam == new IntPtr(CTRL_WPARAM) && msg.Msg == WM_KEYDOWN && msg.WParam == new IntPtr(VKEY_WPARAM))
        {
            // Do not let this Control process Ctrl-V, we'll do it manually.
            HtmlEditorControl parentControl = this.Parent as HtmlEditorControl;
            if (parentControl != null)
            {
                parentControl.ExecuteCommandDocument("Paste");
            }
            return true;
        }
        prevMsg = msg;
        return base.PreProcessMessage(ref msg);
    }
}

Here is my custom method to handle Paste commands, yours might do something similar with the Image data from the Clipboard.

    internal void ExecuteCommandDocument(string command, bool prompt)
    {
        try
        {
            // ensure command is a valid command and then enabled for the selection
            if (document.queryCommandSupported(command))
            {
                if (command == HTML_COMMAND_TEXT_PASTE && Clipboard.ContainsImage())
                {
                    // Save image to user temp dir
                    String imagePath = Path.GetTempPath() + "\\" + Path.GetRandomFileName() + ".jpg";
                    Clipboard.GetImage().Save(imagePath, System.Drawing.Imaging.ImageFormat.Jpeg);
                    // Insert image href in to html with temp path
                    Uri uri = null;
                    Uri.TryCreate(imagePath, UriKind.Absolute, out uri);
                    document.execCommand(HTML_COMMAND_INSERT_IMAGE, false, uri.ToString());
                    // Update pasted id
                    Guid elementId = Guid.NewGuid();
                    GetFirstControl().id = elementId.ToString();
                    // Fire event that image saved to any interested listeners who might want to save it elsewhere as well
                    if (OnImageInserted != null)
                    {
                        OnImageInserted(this, new ImageInsertEventArgs { HrefUrl = uri.ToString(), TempPath = imagePath, HtmlElementId = elementId.ToString() });
                    }
                }
                else
                {
                    // execute the given command
                    document.execCommand(command, prompt, null);
                }
            }
        }
        catch (Exception ex)
        {
            // Unknown error so inform user
            throw new HtmlEditorException("Unknown MSHTML Error.", command, ex);
        }

    }

Hope someone finds this helpful and doesn't waste a day on it like me today.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜