开发者

How can I trap all mouse events on a control?

开发者 https://www.devze.com 2023-01-11 21:32 出处:网络
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

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 c开发者_StackOverflowontrol 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)
            {
            }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消