绘制ListView列标题分隔符?

时间:2013-12-17 09:20:16

标签: .net vb.net winforms listview user-controls

我正在尝试以details模式绘制Listview的列标题。

此代码:

Public Sub Me_DrawColumnHeader(ByVal sender As Object, ByVal e As DrawListViewColumnHeaderEventArgs) _
Handles Me.DrawColumnHeader

    e.DrawDefault = False ' Set ownerdraw.

    e.Graphics.FillRectangle(Brushes.SteelBlue, e.Bounds)
    e.DrawText()

End Sub

产生这个:

http://img189.imageshack.us/img189/8150/k5nm.jpg

但是我想绘制像windows一样的垂直分隔线:

http://img571.imageshack.us/img571/7185/1iz9.jpg

  

更新:

我找到了一种方法来绘制它们:

e.Graphics.DrawLine(Pens.Black, e.Bounds.X, e.Bounds.Y, e.Bounds.Left, e.Bounds.Right)

但似乎没有相同的锚。

还注意到我的标题文本太靠近分隔线并且也高于原始的windows listview:

比较他们:

自有:

enter image description here

原件:

enter image description here

这是我用来绘制文字的代码:

    e.DrawDefault = False ' Set ownerdraw.

    Dim strFormat As New StringFormat()

    If e.Header.TextAlign = HorizontalAlignment.Center Then
        strFormat.Alignment = StringAlignment.Center
    ElseIf e.Header.TextAlign = HorizontalAlignment.Right Then
        strFormat.Alignment = StringAlignment.Far
    End If

    e.Graphics.FillRectangle(New SolidBrush(Color.FromArgb(120, 147, 73)), e.Bounds)
    e.Graphics.DrawString(e.Header.Text, e.Font, Brushes.Black, e.Bounds, strFormat)
    e.Graphics.DrawLine(Pens.Black, e.Bounds.X, e.Bounds.Y, e.Bounds.Left, e.Bounds.Right)

1 个答案:

答案 0 :(得分:2)

这就是DrawListViewColumnHeaderEventArgs .DrawBackground .DrawText 方法在没有视觉样式的情况下呈现的方式:

e.DrawDefault = False  ' Set ownerdraw.


'BACKGROUND

Using brush As Brush = New SolidBrush(e.BackColor)
    e.Graphics.FillRectangle(brush, e.Bounds)
End Using

Dim bounds As Rectangle = e.Bounds

bounds.Width -= 1
bounds.Height -= 1

e.Graphics.DrawRectangle(SystemPens.ControlDarkDark, bounds)

bounds.Width -= 1
bounds.Height -= 1

e.Graphics.DrawLine(SystemPens.ControlLightLight, bounds.X, bounds.Y, bounds.Right, bounds.Y)
e.Graphics.DrawLine(SystemPens.ControlLightLight, bounds.X, bounds.Y, bounds.X, bounds.Bottom)
e.Graphics.DrawLine(SystemPens.ControlDark, (bounds.X + 1), bounds.Bottom, bounds.Right, bounds.Bottom)
e.Graphics.DrawLine(SystemPens.ControlDark, bounds.Right, (bounds.Y + 1), bounds.Right, bounds.Bottom)

'TEXT

Dim textAlign As HorizontalAlignment = e.Header.TextAlign
Dim flags As TextFormatFlags = If((textAlign = HorizontalAlignment.Left), TextFormatFlags.GlyphOverhangPadding, If((textAlign = HorizontalAlignment.Center), TextFormatFlags.HorizontalCenter, TextFormatFlags.Right))

'(I added this line)
flags = (flags Or TextFormatFlags.VerticalCenter)

Dim text As String = e.Header.Text
Dim width As Integer = TextRenderer.MeasureText(" ", e.Font).Width
bounds = Rectangle.Inflate(e.Bounds, -width, 0)
TextRenderer.DrawText(e.Graphics, [text], e.Font, bounds, e.ForeColor, flags)