我正在尝试以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
产生这个:
但是我想绘制像windows一样的垂直分隔线:
更新:
我找到了一种方法来绘制它们:
e.Graphics.DrawLine(Pens.Black, e.Bounds.X, e.Bounds.Y, e.Bounds.Left, e.Bounds.Right)
但似乎没有相同的锚。
还注意到我的标题文本太靠近分隔线并且也高于原始的windows listview:
比较他们:
自有:
原件:
这是我用来绘制文字的代码:
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)
答案 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)