日期:2014-05-18  浏览次数:21073 次

求助怎么修改,或者重写webbrowser控件的复制,粘贴
是在按键事件那里吗?或者是重写窗口消息?
外加顺便求替换webbrowser右键的菜单栏的方法,希望能找到个清晰一点的例子,我网上搜的都挺乱的。

------解决方案--------------------
C# code

    private void Form1_Load(object sender, EventArgs e)
    {
        webBrowser1.DocumentCompleted += (s, e1) =>
        {
            webBrowser1.Document.Body.DetachEventHandler("oncopy", OnCopy);
            webBrowser1.Document.Body.AttachEventHandler("oncopy", OnCopy);
            webBrowser1.Document.Body.DetachEventHandler("onpaste", OnPaste);
            webBrowser1.Document.Body.AttachEventHandler("onpaste", OnPaste);
        };
            
        webBrowser1.Navigate("www.sina.com");
    }

    void OnCopy(object sender, EventArgs e)
    {
        dynamic doc = webBrowser1.Document.DomDocument;
        string text = doc.selection.createRange().text;
        MessageBox.Show("copying text:" + text);
    }

    void OnPaste(object sender, EventArgs e)
    {
        string text = Clipboard.GetText();
        Clipboard.SetData("Text", "paste text:" + text);
    }

------解决方案--------------------
webBrowser1.DocumentText = "<html><body></body></html>";
IHTMLDocument2 doc = webBrowser1.Document.DomDocument as IHTMLDocument2;
doc.designMode = "On";

Application.DoEvents();
webBrowser1.Document.Body.DetachEventHandler("oncopy", OnCopy);
webBrowser1.Document.Body.AttachEventHandler("oncopy", OnCopy);
webBrowser1.Document.Body.DetachEventHandler("onpaste", OnPaste);
webBrowser1.Document.Body.AttachEventHandler("onpaste", OnPaste);