这很不寻常,你几乎总是
真正地
注意单击了哪个控件。并有一个mousedown事件,根据单击的控件执行特定操作。但是,您可以在将输入事件发送到控件本身之前捕获它们。您需要使用IMessageFilter接口。最好用代码示例解释:
Public Class Form1
Implements IMessageFilter
Public Sub New()
InitializeComponent()
Application.AddMessageFilter(Me)
End Sub
Protected Overrides Sub OnFormClosed(ByVal e As System.Windows.Forms.FormClosedEventArgs)
Application.RemoveMessageFilter(Me)
End Sub
Public Function PreFilterMessage(ByRef m As System.Windows.Forms.Message) As Boolean Implements System.Windows.Forms.IMessageFilter.PreFilterMessage
REM catch WM_LBUTTONDOWN
If m.Msg = &H201 Then
Dim pos As New Point(m.LParam.ToInt32())
Dim ctl As Control = Control.FromHandle(m.HWnd)
If ctl IsNot Nothing Then
REM do something...
End If
REM next line is optional
Return False
End If
End Function
End Class
请注意,此筛选器在
全部的
应用程序中的表单。如果只想使ctl值特定于一个表单,则需要对其进行筛选。