鼠标悬停崩溃/扩展标志符号上的自定义树视图绘制错误

时间:2011-12-09 12:21:41

标签: vb.net treeview mouseover ondraw glyph

我在绘制所有模式(例如下面的代码)中覆盖树视图的节点绘制事件。

 Protected Overrides Sub OnDrawNode(ByVal e As System.Windows.Forms.DrawTreeNodeEventArgs)
    Try
        Dim Indent = e.Node.Level * Me.Indent + 32
        Dim font = Me.Font
        'draw selected
        If e.State And TreeNodeStates.Selected Then
            Dim rect As New Rectangle(0, e.Bounds.Location.Y, Me.Width - 1, e.Bounds.Height - 1)
            e.Graphics.FillRectangle(Brushes.AliceBlue, rect)
            e.Graphics.DrawRectangle(Pens.DarkSlateBlue, rect)
        End If


        'draw status icon
        e.Graphics.DrawImage(Me.ImageList.Images(e.Node.ImageIndex), New Point(e.Bounds.X + indent - Me.ImageList.ImageSize.Width + 2, e.Bounds.Y + ((Me.ItemHeight / 2) - (Me.ImageList.ImageSize.Height / 2))))

        'draw collapse glyph
        If e.Node.Nodes.Count > 0 Then
            Dim element As VisualStyleElement
            Dim glyphRect = New Rectangle(e.Bounds.Location.X + 2 + e.Node.Level * Me.Indent, e.Bounds.Location.Y + 8, 16, 16)
            If e.Node.IsExpanded Then
                element = VisualStyleElement.TreeView.Glyph.Opened
            Else
                element = VisualStyleElement.TreeView.Glyph.Closed
            End If

            Dim renderer As New VisualStyleRenderer(element)
            renderer.DrawBackground(e.Graphics, glyphRect)
        End If

        If e.Node.Level.Equals(0) Then
            font = New Font(Me.Font.Name, 12, FontStyle.Regular)
            e.Graphics.DrawString(e.Node.Text, font, Brushes.MidnightBlue, New Point(indent + 5, e.Bounds.Location.Y + 5), New StringFormat())
        ElseIf e.Node.Level.Equals(1) Then
            'action
            Dim params = CType(e.Node, ActionNode).Params

            Dim x = indent + 5
            e.Graphics.DrawString(e.Node.Text, Me.Font, Brushes.Black, New Point(x, e.Bounds.Location.Y + 2), New StringFormat())
            For Each param In params
                e.Graphics.DrawString(param.Key & ":", Me.Font, Brushes.DarkSlateBlue, New Point(x, e.Node.Bounds.Location.Y + 15))
                x += e.Graphics.MeasureString(param.Key & ":", Me.Font).Width - 1
                e.Graphics.DrawString(param.Value, Me.Font, Brushes.SlateGray, New Point(x, e.Node.Bounds.Location.Y + 15))
                x += e.Graphics.MeasureString(param.Value, Me.Font).Width
            Next

        ElseIf e.Node.Level.Equals(2) Then
            'assertion
            Dim params = CType(e.Node, AssertionNode).Params

            Dim x = indent + 5
            e.Graphics.DrawString(e.Node.Text, Me.Font, Brushes.Black, New Point(x, e.Bounds.Location.Y + 2), New StringFormat())
            For Each param In params
                e.Graphics.DrawString(param.Key & ":", Me.Font, Brushes.DarkSlateBlue, New Point(x, e.Node.Bounds.Location.Y + 15))
                x += e.Graphics.MeasureString(param.Key & ":", Me.Font).Width - 1
                e.Graphics.DrawString(param.Value, Me.Font, Brushes.SlateGray, New Point(x, e.Node.Bounds.Location.Y + 15))
                x += e.Graphics.MeasureString(param.Value, Me.Font).Width
            Next
        End If
    Catch ex As Exception

    End Try
End Sub

这完全按照我想要的方式绘制树视图但是由于某种原因,你将鼠标悬停在打开/关闭元素上,节点似乎被重绘,但是在最后一次重绘的顶部,导致文本看起来粗体和任何周围的轮廓图片。这只会发生,但是如果没有选择节点,如果选择了节点,那么一切都很好。抱歉,新用户无法发布屏幕转储。

我不确定你是否可以挂钩鼠标悬停字形事件来使控件无效甚至在绘制事件中检测发件人,但现在我已经出了想法。

尝试:

  • 在绘制节点之前清除绘图上的图形对象
  • 设置背景矩形并像选择时一样绘制节点

1 个答案:

答案 0 :(得分:1)

我真的可以猜到,因为你无法发布图片,而你所包含的代码还不完整(ActioNode?AssertionNode?)。

我知道你提到清除后台,但你发布的代码没有清除节点区域。尝试将其更改为类似的内容,看看它是否有效:

Dim rect As New Rectangle(0, e.Bounds.Top, Me.ClientSize.Width - 1, e.Bounds.Height - 1)
If e.State And TreeNodeStates.Selected Then
  e.Graphics.FillRectangle(Brushes.AliceBlue, rect)
  e.Graphics.DrawRectangle(Pens.DarkSlateBlue, rect)
Else
  e.Graphics.FillRectangle(SystemBrushes.Window, rect)
End If

你为什么忽略所有例外?

您还需要处理字体。

相关问题