VB.NET 面板中的自定义菜单
VB.NET Custom Menu in Panel
您好,我正在像下面这张图片 (link) 这样的面板中实现自定义菜单..
这里是 link http://i.imgur.com/5OlRk9c.png
我的问题是,除了菜单面板和按钮(红色圆圈内)之外,我如何检测到用户点击了表单的其他部分。
我已经使用了 LostFocus 事件,但没有任何反应。
请帮忙。
您可以在鼠标消息通过 IMessageFilter 路由到控件之前捕获鼠标消息。然后您可以检查光标位置是在面板边界之内还是之外。这是 Panel1 的一个简单示例:
Public Class Form1
Private WithEvents filter As New MyFilter
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Application.AddMessageFilter(filter)
End Sub
Private Sub filter_LeftClick() Handles filter.LeftClick
Dim rc As Rectangle = Panel1.RectangleToScreen(Panel1.ClientRectangle)
If Not rc.Contains(Cursor.Position) Then
Debug.Print("Click outside of Panel1")
End If
End Sub
Private Class MyFilter
Implements IMessageFilter
Public Event LeftClick()
Private Const WM_LBUTTONDOWN As Integer = &H201
Public Function PreFilterMessage(ByRef m As System.Windows.Forms.Message) As Boolean Implements System.Windows.Forms.IMessageFilter.PreFilterMessage
Select Case m.Msg
Case WM_LBUTTONDOWN
RaiseEvent LeftClick()
End Select
Return False
End Function
End Class
End Class
您好,我正在像下面这张图片 (link) 这样的面板中实现自定义菜单..
这里是 link http://i.imgur.com/5OlRk9c.png
我的问题是,除了菜单面板和按钮(红色圆圈内)之外,我如何检测到用户点击了表单的其他部分。
我已经使用了 LostFocus 事件,但没有任何反应。
请帮忙。
您可以在鼠标消息通过 IMessageFilter 路由到控件之前捕获鼠标消息。然后您可以检查光标位置是在面板边界之内还是之外。这是 Panel1 的一个简单示例:
Public Class Form1
Private WithEvents filter As New MyFilter
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Application.AddMessageFilter(filter)
End Sub
Private Sub filter_LeftClick() Handles filter.LeftClick
Dim rc As Rectangle = Panel1.RectangleToScreen(Panel1.ClientRectangle)
If Not rc.Contains(Cursor.Position) Then
Debug.Print("Click outside of Panel1")
End If
End Sub
Private Class MyFilter
Implements IMessageFilter
Public Event LeftClick()
Private Const WM_LBUTTONDOWN As Integer = &H201
Public Function PreFilterMessage(ByRef m As System.Windows.Forms.Message) As Boolean Implements System.Windows.Forms.IMessageFilter.PreFilterMessage
Select Case m.Msg
Case WM_LBUTTONDOWN
RaiseEvent LeftClick()
End Select
Return False
End Function
End Class
End Class