2013-12-17 42 views
2

我試圖在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

UPDATE:

我找到了一種方法來吸引他們:

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

,但似乎不具有相同的錨。

還注意到,我的頭文字太靠近隔離線,也比原來的Windows列表視圖上:

對它們進行比較:

自己:

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

這是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) 
+0

這是一個靈活的解決方案來定製所有的東西,非常感謝 – ElektroStudios