现在treeview控件内单击右键弹出菜单,
可是在treeview的任何位置单击右键都弹出菜单,
能不能实现只在选中的节点下单击右键弹出菜单?在其他位置单击右键不弹出菜单?

解决方案 »

  1.   

    使用控件的.HitTest方法
    Private Sub TreeView1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
        Dim a As Node
        
        If Button = vbRightButton Then
            With TreeView1
                Set a = .HitTest(x, y)
                If a Is Nothing Then       '检查当前位置上是否有节点
                
                Else
                    MsgBox a.Text          '有节点的处理(如弹出菜单)
                End If
                
            End With
        End If
    End Sub