2

I got a WebBrowser Control in a Windows Form and i want to use the middle mouse button click to add a new tab to my Browser. The only problem is that everytime i use the middle mouse button, the arrows to move the page appear.

So how can i disable this move/drag command only for the clicks on my links?

ElektroStudios
  • 19,105
  • 33
  • 200
  • 417
Florian Leitgeb
  • 15,657
  • 6
  • 31
  • 40
  • 2
    That is most likely a function of your mouse driver software, check the Mouse settings in Control Panel – Mark Hall Oct 19 '12 at 16:08
  • So why is Mozilla doing it the right way? Try it out, middle click on a link and it will open in new background tab, do it somewhere else in the browser and you can move the page. – Florian Leitgeb Oct 19 '12 at 16:26

2 Answers2

1

try this: There are two different parts to this solution. The first is pretty easy - just set an event handler for the MouseDown event of your browser control:

 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
                }
            }

but there were also some javascript-heavy websites on which this solution did not work. For those, I found another solution involving injecting javascript into the document which will prevent the middle-click:

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);

UPDATE

The JavaScript injection methodology can be improved by following the suggested way to do it using managed code only: stackoverflow.com/a/6222430/1248295

This is an alternative example implementation of the javascript injection, wrote in 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

UPDATE 2

Inject this code instead:

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

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

Community
  • 1
  • 1
Shahrooz Ansari
  • 2,637
  • 1
  • 13
  • 21
  • Pretty awesome, thank you. But I am not working on this project anymore. Hope it helps other people. – Florian Leitgeb May 04 '17 at 08:34
  • 1st solution doesn't works, at least for the websites that I tried, handling **HtmlDocument** / **HtmlElement** classes **MouseDown**, **MouseUp** and **Click** events all with a **e.ReturnValue = false** it just does nothing of nothing. – ElektroStudios May 04 '17 at 08:51
  • 2nd solution doesn't work as expected. After injecting the JS code yes it disables the webpage dragging funcionality, which is what we wanted to avoid,however it doesn't work for all html elements,I still be able to click some buttons in webpages with middle button and it drags the page. – ElektroStudios May 04 '17 at 08:52
  • Also and most important, that JS code prevents me for clicking left button to use the WebBrowser scrollbars (the scrollbars that are drawn by the WebBrowser instance), that is very distusting, I need to make double-click to use scrollbars, and maybe this also could mean that code will produce more strange behaviors than fixes. I think injecting JS could be the way to go, I hope so, however not with that code that is imperfect or the way it is implemented,that alters the normal left-click behavior. – ElektroStudios May 04 '17 at 08:54
  • I hope someone could improve the provided JavaScript code to meet the requisites of this question which only are "disable the webpage dragging funcionalty that occurs when the middle mouse button is pressed". I noticed in that code it is not necessary to add handlers for onmouseup and onclick, it just needs to handle onmousedown. And as my last words: the JS injection can be improved following the suggested way to do it using managed code only: http://stackoverflow.com/a/6222430/1248295 – ElektroStudios May 04 '17 at 08:56
  • 1
    THANK YOU FOR YOUR UPDATING – Shahrooz Ansari May 04 '17 at 09:10
  • 1
    No, thanks to you for sharing that javascript code!, which works... not as expected, but it works on some way. Dude I wish if you have javascript knowledges and maybe you are able to fix that code. The only thing I need to mark this answer as the accepted one is that the provided code lets me use the left-click properly on the WebBrowser scrollbars. Regards! – ElektroStudios May 04 '17 at 09:17
  • @FlorianLeitgeb please accept the answer for helping the others Thank you ;-) – Shahrooz Ansari May 04 '17 at 10:22
-1

The move icon displayed upon middle mouse click is under control of your mouse settings. But you can detect middle mouse click on your WebBrowser control.

Register DocumentCompleted event for your WebBrowser control and use the following code:

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
     }
}
Mohsen Afshin
  • 13,273
  • 10
  • 65
  • 90
  • 1
    The problem is not to detect the middle mouse button event, the problem is that this move cursor appears. And i want to disable it for some actions, but not for everyone. – Florian Leitgeb Oct 19 '12 at 20:44