none
怎么让TreeView节点DropOver事件时,选中节点既有图标,又有节点名称,就像qq那样 RRS feed

答案

  • 你好 LiZhePro,

    我这里做了一个例子但好像效率有点影响,你先看看下面的代码。
    Public Class Form5
        Private dragNode As TreeNode

        Public Sub New()
            InitializeComponent()
            TreeView1.AllowDrop = True
        End Sub

        Private Sub TreeView1_DragOver(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TreeView1.DragOver
            TreeView1.Refresh()
            e.Effect = DragDropEffects.Move
            Dim g As Graphics = TreeView1.CreateGraphics()
            Dim p As Point = New Point(e.X, e.Y)
            Dim clientP As Point = TreeView1.PointToClient(p)
            g.DrawString(dragNode.Text, TreeView1.Font, Brushes.Blue, New PointF(CType(clientP.X, Single), CType(clientP.Y, Single)))
        End Sub

        Private Sub TreeView1_ItemDrag(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ItemDragEventArgs) Handles TreeView1.ItemDrag
            DoDragDrop(e.Item, DragDropEffects.Move)
        End Sub

        Private Sub TreeView1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TreeView1.MouseDown
            If TreeView1.SelectedNode IsNot Nothing Then
                dragNode = TreeView1.SelectedNode
            End If
        End Sub

        Private Sub TreeView1_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TreeView1.DragDrop
            TreeView1.Refresh()
        End Sub
    End Class

    你抓以上几个事件去跟着MOuse的移动动态DrawString到TreeView上就有你要的那个效果了。但因为要清掉Mouse移动过程中Draw的string,只能调用TreeView.Refresh方法,这样会影响效率。现在我的例子只是DrawString,对于Image的也可以相同处理。

    希望对你有帮助!

    Sincerely,
    Kira Qian


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework!
    • 已标记为答案 LiZhePro 2010年1月31日 5:17
    2010年1月29日 8:47