取消激活鼠标中键在Windows浏览器窗体中移动

时间:2012-10-19 16:02:30

标签: c# winforms forms browser

我在Windows窗体中有一个WebBrowser控件,我想使用鼠标中键单击向我的浏览器添加一个新选项卡。唯一的问题是,每当我使用鼠标中键时,会出现移动页面的箭头。

那么我如何才能为链接点击禁用此移动/拖动命令?

2 个答案:

答案 0 :(得分:1)

试试这个: 该解决方案有两个不同的部分。第一个很简单 - 只需为浏览器控件的MouseDown事件设置一个事件处理程序:

 private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
                {
                    if (webBrowser1.Document != null)
                    {
                        webBrowser1.Document.MouseDown += Document_MouseDown;
                    }
                }

 private void Document_MouseDown(object sender, HtmlElementEventArgs e)
            {
                if (e.MouseButtonsPressed == System.Windows.Forms.MouseButtons.Middle)
                {
                    e.ReturnValue = false;

                    // Your custom code
                }
            }

但也有一些重量级的网站,这个解决方案不起作用。对于那些,我找到了另一个解决方案,涉及将javascript注入到文档中,这将阻止中间点击:

HtmlElement head = browser.Document.GetElementsByTagName("head")[0];
HtmlElement mscript = browser.Document.CreateElement("script");
IHTMLScriptElement element = (IHTMLScriptElement)mscript.DomElement;
element.text = "function handleMouseEvent(e) { "
               + "var evt = (e==null ? event:e); "
               + "if ( e.which == 2 ) e.preventDefault(); "
               + "return true; } "
               + "document.onmousedown = handleMouseEvent; "
               + "document.onmouseup   = handleMouseEvent; "
               + "document.onclick     = handleMouseEvent; ";
head.AppendChild(mscript);

更新

可以通过遵循建议的方式使用托管代码来改进JavaScript注入方法: stackoverflow.com/a/6222430/1248295

这是javascript注入的另一个示例实现,在VB.NET中写道:

        Private ReadOnly handleMouseEventJs As String =
"
function HandleMouseEvent(e) {
    var evt = (e==null ? event:e); 
    if (e.which == 2) e.preventDefault(); 
    return true;
} 

document.onmousedown = HandleMouseEvent;
// These events below seems are not necessary to handle for this purpose.
// document.onmouseup = HandleMouseEvent;
// document.onclick   = HandleMouseEvent;
"

Private Sub WebBrowser1_Navigated(sender As Object, e As WebBrowserNavigatedEventArgs) _
Handles WebBrowser1.Navigated

    Dim wb As WebBrowser = DirectCast(sender, WebBrowser)
    Dim doc As HtmlDocument = wb.Document
    Dim head As HtmlElement = doc.GetElementsByTagName("head")(0)
    Dim js As HtmlElement = doc.CreateElement("script")
    js.SetAttribute("text", handleMouseEventJs)
    head.AppendChild(js)

    ' This method call seems not necessary at all, it works fine without invocking it.
    ' doc.InvokeScript("HandleMouseEvent", Nothing)

End Sub

更新2

改为输入此代码:

document.body.onmousedown = function(e) { if (e.button === 1) return false; }

https://stackoverflow.com/a/30423534/1248295

答案 1 :(得分:-1)

鼠标中键点击时显示的移动图标受鼠标设置控制。但您可以在WebBrowser控件上检测鼠标中键。

DocumentCompleted控件注册WebBrowser个事件并使用以下代码:

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    webBrowser1.Document.MouseDown += Document_MouseDown;
}

void Document_MouseDown(object sender, HtmlElementEventArgs e)
{
     if (e.MouseButtonsPressed == System.Windows.Forms.MouseButtons.Middle)
     {
        // code to add tab or do sth else
     }
}