2

I'm trying to setup a WebBrowser where I can open up new tabs by middle mouse clicking links.

First tried the Click/MouseDown events for both the WebBrowser and WebBrowser.Document which only trigger on left clicks. Then I tried making overidding the WndProc procedure on the WebBrowser where I ran into my problem. I can get middle mouse presses this way and then send a click through to the link, but it also begins autoscrolling. Also tried to see if I could override WndProc on the WebBrowser.Document, but it is a sealed class.

I've tried looking for alternatives to the WebBrowser component because I'm not totally happy with using it in the first place. Options for replacing it seem to be seems to be either packaging webkit/gecko or build my own browser ontop of a html rendering component. I didn't like webkit/gecko much because of the extra size it adds to the program. I haven't had much luck with finding a html rendering component that does what I want.

carker888
  • 21
  • 2
  • 2
    You cannot reasonably disable mouse wheel scrolling for a web browser. Your idea to use a middle-click is just fatally flawed. – Hans Passant Apr 09 '11 at 01:38
  • I've been trying to use a transparent control over the top of the webbrowser to intercept all input. Using some info I found [here](http://stackoverflow.com/questions/112224/click-through-transparency-for-visual-c-window-forms) I can let all input pass through the transparent control, but can't figure out how to filter out the middle mouse clicks. I probably need to just sit down and actually learn how messages work instead of copying and pasting code. – carker888 Apr 10 '11 at 16:56
  • Hans, your comment shows total lack of understanding of the question. Carker888 is trying to replicate the behavior of every other normal browser, and not trying to disable wheel scrolling. – Daniel Nov 27 '13 at 09:57

1 Answers1

0

I know this is super old, but I recently had a very difficult time figuring out how to do this and thought I would share. 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:

browser.Document.MouseDown += new HtmlEventHandler(Browser_MouseDown);

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

         // Your custom code
     }
}

I found that the above solution prevented the normal middle-click action on most webpages, 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);

I put this on the DocumentCompleted event for the webbrowser control. The above solution appears to prevent the default middle-click action on all sites.