TreeView——DrawNode请帮助修改一点错误

a854468521 2009-06-21 08:19:04
该段代码来自微软的示例,可以绘制节点的文字、连线。
目前有以下问题:
1:想添加节点图标,绘制位置不正确;
2:绘制时总是在顶端跳到字符,TreeView.BeginUpdate 、TreeView.EndUpdate 放在哪里适合?

Public Sub New()
' 此调用是 Windows 窗体设计器所必需的。
InitializeComponent()
' 在 InitializeComponent() 调用之后添加任何初始化。
AddHandler Me.MouseDown, AddressOf myTreeView_MouseDown
End Sub
Private Sub Tmb_DrawNode(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawTreeNodeEventArgs) Handles Me.DrawNode
Dim Image As Drawing.Image = Me.ImageList1.Images(e.Node.ImageIndex)
' Draw the background and node text for a selected node.
If (e.State And TreeNodeStates.Selected) <> 0 Then

' Draw the background of the selected node. The NodeBounds
' method makes the highlight rectangle large enough to
' include the text of a node tag, if one is present.
e.Graphics.FillRectangle(Brushes.Green, NodeBounds(e.Node))

' Retrieve the node font. If the node font has not been set,
' use the TreeView font.
Dim nodeFont As Font = e.Node.NodeFont
If nodeFont Is Nothing Then
nodeFont = CType(sender, TreeView).Font
End If

' Draw the node text.
e.Graphics.DrawString(e.Node.Text, nodeFont, Brushes.White, _
e.Bounds.Right - 2, e.Bounds.Top)
图片位置不正确
e.Graphics.DrawImage(Image, e.Bounds.Right, e.Bounds.Top)
' Use the default background and node text.
Else
e.DrawDefault = True
End If

' If a node tag is present, draw its string representation
' to the right of the label text.
If Not (e.Node.Tag Is Nothing) Then
e.Graphics.DrawString(e.Node.Tag.ToString(), Me.Font, _
Brushes.Yellow, e.Bounds.Right + 2, e.Bounds.Top)
图片位置不正确
e.Graphics.DrawImage(Image, e.Bounds.Right, e.Bounds.Top)

End If

' If the node has focus, draw the focus rectangle large, making
' it large enough to include the text of the node tag, if present.
If (e.State And TreeNodeStates.Focused) <> 0 Then
Dim focusPen As New Pen(Color.Black)
Try
focusPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot
Dim focusBounds As Rectangle = NodeBounds(e.Node)
focusBounds.Size = New Size(focusBounds.Width - 1, _
focusBounds.Height - 1)
e.Graphics.DrawRectangle(focusPen, focusBounds)
Finally
focusPen.Dispose()
End Try
End If
End Sub

Private Sub myTreeView_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
Dim clickedNode As TreeNode = Me.GetNodeAt(e.X, e.Y)
If NodeBounds(clickedNode).Contains(e.X, e.Y) Then
Me.SelectedNode = clickedNode
End If
End Sub 'myTreeView_MouseDown

' Returns the bounds of the specified node, including the region
' occupied by the node label and any node tag displayed.
Private Function NodeBounds(ByVal node As TreeNode) As Rectangle
' Set the return value to the normal node bounds.
Dim bounds As Rectangle = node.Bounds
If Not (node.Tag Is Nothing) Then

' Retrieve a Graphics object from the TreeView handle
' and use it to calculate the display width of the tag.
Dim g As Graphics = Me.CreateGraphics()
Dim tagWidth As Integer = CInt(g.MeasureString( _
node.Tag.ToString(), Me.Font).Width) + 6

' Adjust the node bounds using the calculated value.
bounds.Offset(tagWidth \ 2, 0)
bounds = Rectangle.Inflate(bounds, tagWidth \ 2, 0)
g.Dispose()
End If
Return bounds
End Function 'NodeBounds
...全文
66 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
dylike 2009-06-22
  • 打赏
  • 举报
回复
Public Sub New() 的时候TREEVIEW还没初始化完成呢,所有的结点还没正确放到位置,并不是显示出来之后的位置.
把代码放到form_load试试
a854468521 2009-06-22
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 dylike 的回复:]
Public Sub New() 的时候TREEVIEW还没初始化完成呢,所有的结点还没正确放到位置,并不是显示出来之后的位置.
把代码放到form_load试试
[/Quote]
TreeView.Node是动态添加的,也就是在展开节点时添加到孙级节点,如果一次将节点全部添加,数万个节点(Node.tag自定义了一个结构)会耗很多的资源,在绘制节点时总是在TreeView的顶端。
hanjs 2009-06-21
  • 打赏
  • 举报
回复
关注。
古今多少事 2009-06-21
  • 打赏
  • 举报
回复
楼主解决不了的,俺就甭看了……
a854468521 2009-06-21
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 bw555 的回复:]
ImageList1中你加入图片了么?
e.Node.ImageIndex是否设置了?
[/Quote]
图片是有的,只是位置不正确。
bw555 2009-06-21
  • 打赏
  • 举报
回复
ImageList1中你加入图片了么?
e.Node.ImageIndex是否设置了?
heyu1000 2009-06-21
  • 打赏
  • 举报
回复
VB的呀,这么多不想看,一直用C#,好久没看过VB了,帮顶一下

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧