简体   繁体   中英

How can I trap all mouse events on a control?

I need to capture all mouse events on a .NET's WebBrowser, process them and prevent the WebBrowser from getting them. Is there any way to achieve this? I wonder if there is any way I can handle mouse events if the control is disabled.

You'll have to override WndProc() to intercept the mouse messages. Like this:

using System;
using System.Windows.Forms;

class MyBrowser : WebBrowser {
    protected override void WndProc(ref Message m) {
        if (m.Msg >= 0x200 && m.Msg <= 0x20a) {
            // Handle mouse messages
            //...
        }
        else base.WndProc(ref m);
    }
}

There IS a solution to this. You need to capture the mouse events that are associated with the Document object that is associated with the webBrowser control.

After the DocumentCompleted event occurs, and inside you DocumentCompleted event handler, do the following:

myWebBrowser.Document.MouseDown += new HtmlElementEventHandler(myMouseDown);

and have the related handler:

void myMouseDown(object sender, HtmlElementEventArgs e)

{
    your code to handle the mouse event... such as ...

            if (e.MouseButtonsPressed == MouseButtons.Right)
            {
            }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM