简体   繁体   English

HtmlEditor WinForms上的拦截粘贴事件

[英]Intercept paste event on HtmlEditor WinForms

I'm using a HtmlEditor control inside a Windows Form. 我在Windows窗体内使用HtmlEditor控件。

I got the control from this page: 我从此页面获得控件:

http://windowsclient.net/articles/htmleditor.aspx 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 nothing. 现在,您可以粘贴纯文本和带格式的文本,但是在尝试粘贴图像时它什么也没做。

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. 基本上,我的想法是检测用户何时在编辑器上按Ctrl + V,检查剪贴板中的图像,如果有图像,则将其手动插入到编辑器中。

The problem with this approach is that I cannot get the OnKeyDown or OnKeyPress events of the form to be raised. 这种方法的问题是我无法获取要引发的窗体的OnKeyDown或OnKeyPress事件。

I have the KeyPreview property set to true on the form, but still the events aren't raised. 我在窗体上将KeyPreview属性设置为true,但仍未引发事件。

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. 我也试图子类形式和编辑器(如解释在这里 )拦截WM_PASTE消息,但它也不会被上调。

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. 尝试侦听WM_PASTE消息不起作用,因为基础mshtml控件正在对Ctrl-V进行预处理。 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. 您可以侦听OnKeyDown / Up等以捕获Ctrl-V,但这不会阻止基础控件继续其默认的粘贴行为。 My solution is to prevent the PreProcessing of the Ctrl-V message and then implementing my own Paste behavior. 我的解决方案是防止对Ctrl-V消息进行预处理,然后实现自己的粘贴行为。 To stop the control from PreProcessing the CtrlV message I had to subclass my Control which is AxWebBrowser, 要停止对CtrlV消息进行预处理,我必须将Control子类化为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. 希望有人能对此有所帮助,并且不要像今天这样浪费我一天的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM