2

I have a document loaded into WebBrowser. I want it to stay scrollable and reactive to mouse moves (changing the color at onmouseover and etc), but totally not reactive to mouse clicks and key presses.

How I can do that?

I can't use WebBrowser.Document.Click event. AllowNavigation is also not an option. It suppresses navigation only, but not reactions like scripted ones. I saw PreviewKeyDown event, but I don't see anyway how I can cancel press propagation. Usually we have something like e.Cancel in events like this.

Kosmo零
  • 4,001
  • 9
  • 45
  • 88

2 Answers2

2

I haven't thoroughly tested this, but it should give you a good start:

public partial class Form1 : Form, IMessageFilter
{

    private const int WM_KEYDOWN = 0x0100;

    private const int WM_LBUTTONDOWN = 0x201;
    private const int WM_LBUTTONDBLCLK = 0x0203;
    private const int WM_RBUTTONDOWN = 0x0204;
    private const int WM_RBUTTONDBLCLK = 0x0206;

    public Form1()
    {
        InitializeComponent();
        this.Load += Form1_Load;
    }

    void Form1_Load(object sender, EventArgs e)
    {
        Application.AddMessageFilter(this);
    }

    public bool PreFilterMessage(ref Message m)
    {
        switch(m.Msg)
        {
            case WM_KEYDOWN:
                Control ctl = this.ActiveControl;
                if ((ctl != null) && ctl.Equals(this.WebBrowser))
                {
                    return true;
                }
                break;

            case WM_LBUTTONDOWN:
            case WM_LBUTTONDBLCLK:
            case WM_RBUTTONDOWN:
            case WM_RBUTTONDBLCLK:
                Rectangle rc = this.WebBrowser.RectangleToScreen(new Rectangle(new Point(0, 0), this.WebBrowser.ClientSize));
                return rc.Contains(Cursor.Position);
        }

        return false;
    }

}
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
  • P.S. The scrolling does not work, but I can live with that for now. – Kosmo零 Jul 20 '15 at 16:00
  • Scrolling via MouseWheel still worked on my system...but the scrollbars themselves in the webbrowser window can't be clicked now. Not sure if there is an "easy" way to change that; probably not. – Idle_Mind Jul 20 '15 at 16:13
  • That's right. Scrolling via mouse is working, but browser must have the focus, but it gains focus from clicks, that is now disabled. So to make wheel scrolling work I need to use Tab key to set focus to browser. I thinking what I can do with this. – Kosmo零 Jul 20 '15 at 16:18
0

Maybe wrap everything up in a div and do

document.getElementById('SOMEDIV').addEventListener('click', function(e) {
e.preventDefault(); //Don't do anything on click

});
Bryan Mudge
  • 432
  • 4
  • 12
  • Thank you. Maybe this will work, but I can't modify the document. I just need to stop clicks and presses propagation. – Kosmo零 Jul 20 '15 at 13:49
  • var mouseclickevent = $(document).click(function(e) { e.stopPropagation(); // don't register the click any further e.preventDefault(); //stop the default action return false; }); – Bryan Mudge Jul 20 '15 at 13:54
  • What's the non JQuery solution? I tried to set onclick for body like that: `document.body.onclick = function(e) { e.stopPropagation(); e.preventDefault(); };` but I freely can press some button on the page with that and the script executes. I tried to sey onclick for `document.onclick` too... – Kosmo零 Jul 20 '15 at 14:02
  • try document.getElementsByTagName("body")[0] – Bryan Mudge Jul 20 '15 at 14:07
  • Nothing changed. I did so: `` – Kosmo零 Jul 20 '15 at 14:11
  • It could have something to do with the browser you're using. I just tried this in chrome and firefox and I added an alert to the event. and it triggers but i'm still interacting with the document. I think it's a webkit thing. I'll dig around and see if there's a workaround. – Bryan Mudge Jul 20 '15 at 14:24
  • I don't know if this will help but look at this stack overflow question: http://stackoverflow.com/questions/2739627/jquery-preventdefault-not-working-on-input-click-events – Bryan Mudge Jul 20 '15 at 14:25
  • I still think I should somehow to stop click and press propagation at WebBrowser level. It should receive those first and decide what to do with them. The question at your link didn't helped :O – Kosmo零 Jul 20 '15 at 14:28
  • Yah it looks like the browser receives them but doesn't prevent the default action. I haven't been able to find a workaround to this, seems like some security measure. It might work as expected in an older version of IE – Bryan Mudge Jul 20 '15 at 14:38