簡體   English   中英

如何捕獲控件上的所有鼠標事件?

[英]How can I trap all mouse events on a control?

我需要捕獲.NET的WebBrowser上的所有鼠標事件,進行處理並阻止WebBrowser捕獲它們。 有什么辦法可以做到這一點? 我想知道如果禁用了控件,是否可以通過任何方式處理鼠標事件。

您必須重寫WndProc()才能攔截鼠標消息。 像這樣:

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

有一個解決方案。 您需要捕獲與與webBrowser控件關聯的Document對象關聯的鼠標事件。

在發生DocumentCompleted事件之后,在您的DocumentCompleted事件處理程序內部,執行以下操作:

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

並具有相關的處理程序:

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