【问题标题】:Custom treeview draw bug on mouseover collapse/expand glyph鼠标悬停折叠/展开字形上的自定义树视图绘制错误
【发布时间】:2011-12-12 05:28:09
【问题描述】:

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

 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

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

我不确定您是否可以挂钩到鼠标悬停字形事件以使控制无效,甚至检测绘图事件的发送者,但我现在没有想法。

试过了:

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

【问题讨论】:

  • 一定要画背景,填满整个e.Bounds

标签: vb.net treeview mouseover ondraw glyph


【解决方案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

为什么要忽略所有异常?

你还需要处理你的字体。

【讨论】:

  • 您好,是的,我清除了图形对象,因为它似乎没有任何效果。我不知道为什么我不认为这是选定的唯一不同的代码似乎工作正常的节点。我没有忽略所有异常,我只是将其放入以查看是否或抛出了哪些异常以获取更多详细信息。我只是使用实际的树视图字体不知道我需要处理它,但现在会感谢。最后对与动作/断言节点的混淆感到抱歉。它们只是继承treenode的类,用于添加绘制节点时需要的一些数据。
猜你喜欢
  • 1970-01-01
  • 2016-04-23
  • 2021-05-20
  • 2012-01-19
  • 2013-10-17
  • 2017-11-24
  • 2010-12-15
  • 1970-01-01
  • 2023-04-06
相关资源
最近更新 更多