简体   繁体   English

如何捕获控件上的所有鼠标事件?

[英]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. 我需要捕获.NET的WebBrowser上的所有鼠标事件,进行处理并阻止WebBrowser捕获它们。 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. 您必须重写WndProc()才能拦截鼠标消息。 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. 您需要捕获与与webBrowser控件关联的Document对象关联的鼠标事件。

After the DocumentCompleted event occurs, and inside you DocumentCompleted event handler, do the following: 在发生DocumentCompleted事件之后,在您的DocumentCompleted事件处理程序内部,执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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