VB.Net TreeView NodeMouse点击鼠标中键

时间:2013-06-14 20:26:40

标签: vb.net visual-studio-2010 treeview mouseevent mouseclick-event

我一直在使用NodeMouseClick事件来处理我的TreeNodes上的左右点击。现在我想为中间点击添加效果,但NodeMouseClick事件似乎不会在中间点击时触发。这是一个已知的错误,还是它应该正常工作,我只是做错了什么?如果它是一个错误(或者只是想以这种方式运行),如何在TreeNode上进行中间点击以执行特定于该节点的操作?

这是我的一些代码:

Private Sub TreeView1_NodeMouseClick(sender As Object, e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseClick
    If e.Button = Windows.Forms.MouseButtons.Left Then
        Call nodeLeft(e.Node)
    ElseIf e.Button = Windows.Forms.MouseButtons.Middle Then
        Call nodeMiddle(e.Node)
    ElseIf e.Button = Windows.Forms.MouseButtons.Right Then
        Call nodeRight(e.Node)
    End If
End Sub

1 个答案:

答案 0 :(得分:1)

您可以尝试以下版本:

Public Class MyTreeView
  Inherits TreeView

  Private Const WM_MBUTTONDOWN As Integer = &H207

  Protected Overrides Sub WndProc(ByRef m As Message)
    MyBase.WndProc(m)
    If m.Msg = WM_MBUTTONDOWN Then
      Dim p As Point = Me.PointToClient(MousePosition)
      Dim mouseNode As TreeNode = Me.GetNodeAt(p)
      If mouseNode IsNot Nothing Then
        Me.OnNodeMouseClick(New TreeNodeMouseClickEventArgs(mouseNode, MouseButtons.Middle, 1, p.X, p.Y))
      End If
    End If
  End Sub
End Class

它将使用为Button属性设置的中间值触发NodeMouseClick事件。它不会选择节点。为此,请在OnNodeMouseClick调用上方添加行Me.SelectedNode = mouseNode