1

以下のコードのように、すべて描画モードでツリービューのノード描画イベントをオーバーライドしています。

 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

これは私が望むとおりにツリー ビューを描画しますが、なんらかの理由で open/close 要素の上にマウスを置くと、ノードが再描画されるように見えますが、最後の再描画の上にテキストが太字になり、画像の周りにアウトラインが表示されます。これは、ノードが選択されていない場合にのみ発生し、選択されている場合はすべて問題ありません。申し訳ありませんが、新しいユーザーはスクリーン ダンプを投稿できません。

マウスオーバーグリフイベントにフックして、描画イベントで送信者を検出することさえ無効にすることができるかどうかはわかりませんが、今はアイデアがありません。

試した:

  • ノードを描画する前に描画時にグラフィックス オブジェクトをクリアする
  • 背景の四角形を設定し、選択時と同じようにノードを描画します
4

1 に答える 1

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

なぜすべての例外を無視するのですか?

また、フォントを破棄する必要があります。

于 2011-12-09T14:55:22.527 に答える