面板中的VB.NET自定义菜单

时间:2015-08-18 03:21:34

标签: vb.net winforms vb.net-2010 panels

您好我在下面这个图片(链接)的面板中实现自定义菜单。

继承人http://i.imgur.com/5OlRk9c.png

链接

我的问题是,如何检测用户点击表单的其他部分,除了菜单面板和按钮(红色圆圈内)。

我已经使用了LostFocus事件,但没有任何反应。

请帮忙。

1 个答案:

答案 0 :(得分:0)

您可以在通过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