1

i am trying to draw sort arrows on list view column header along with the default visual styles

so far i have got this

Private Sub List_DrawColumnHeader(sender As Object, e As DrawListViewColumnHeaderEventArgs) Handles List.DrawColumnHeader

    e.DrawDefault = True
    If e.ColumnIndex = selectedIndex Then
        e.Graphics.DrawImage(ImageList1.Images(1), CType(e.Bounds.Left + e.Bounds.Width / 2, Single) - 5, -2)
    End If

End Sub

but the visual style is drawn over the arrow somehow so i figured i could try this :

Private Sub List_DrawColumnHeader(sender As Object, e As DrawListViewColumnHeaderEventArgs) Handles List.DrawColumnHeader

    e.DrawDefault = True
    If lastDrawn.ColumnIndex = selectedIndex Then
        e.Graphics.DrawImage(ImageList1.Images(1), CType(lastDrawn.Bounds.Left + lastDrawn.Bounds.Width / 2, Single) - 5, -2)
    End If

    lastDrawn=e

End Sub

and it draws the arrow when the next corresponding column is being drawn but with this i cant get it to draw for the last column

Screenshots: enter image description here

enter image description here

enter image description here

4

1 回答 1

2

为了使用 .NET 内置解决方案来显示列表视图列标题的自定义图标,您需要:

  • 创建一个图像列表
  • 向其中添加三个图像(向上/向下箭头和空)
  • 将图像列表绑定到 ListView 控件
  • 绑定到ListView 控件的ColumnClick 事件
  • 对列进行排序时,根据排序方向设置当前排序列的ImageKey 属性

这个示例类(一个简单的表单)展示了如何正确设置图像,而不是使用 ListView 标题列的自定义绘图。

它不执行任何排序!(如何实现 ListViewSorter 显示在这篇MSDN 文章中

在对列进行排序后,您需要实现自定义 ListView-Sorter 类并从中检索图像或图像键。

Public Class SimpleForm
    Inherits Form

    Private sortItems = New ImageList()
    Dim lv As ListView = New ListView()
    Dim so = System.Windows.Forms.SortOrder.Ascending

    Public Sub New()
        ' create columns, items and ListView
        Dim columns = New List(Of ColumnHeader)
        Dim c1 = New ColumnHeader()
        c1.Name = "c1"
        c1.Text = "Name"
        Dim c2 = New ColumnHeader()
        c2.Name = "c2"
        c2.Text = "Type"
        columns.Add(c1)
        columns.Add(c2)

        Dim items = New List(Of ListViewItem)
        Dim i1 = New ListViewItem("Terminator")
        i1.SubItems.Add("T1000")
        Dim i2 = New ListViewItem("Terminator")
        i2.SubItems.Add("T10")
        Dim i3 = New ListViewItem("J.C.")
        i3.SubItems.Add("Human")
        items.Add(i1)
        items.Add(i2)
        items.Add(i3)
        ' init and bind column click
        lv.Columns.AddRange(columns.ToArray())
        lv.Items.AddRange(items.ToArray())
        lv.SmallImageList = sortItems
        lv.View = View.Details
        lv.Dock = DockStyle.Fill
        Controls.Add(lv)

        AddHandler lv.ColumnClick, AddressOf clickEventHandler
        ' init images list 
        sortItems.TransparentColor = System.Drawing.Color.Transparent
        sortItems.Images.Add("up", Image.FromFile("d:\temp\32\arrow_up.gif"))
        sortItems.Images.Add("down", Image.FromFile("d:\temp\32\arrow_down.gif"))
        sortItems.Images.Add("empty", Image.FromFile("d:\temp\32\check.gif"))

    End Sub

    Private Sub clickEventHandler(ByVal o As Object, ByVal e As ColumnClickEventArgs)
        ' Implement a custom ListViewItemSorter and fetch the icon from it!
        ' Set the ListViewItemSorter property to a new ListViewItemComparer  
        ' object. Setting this property immediately sorts the  
        ' ListView using the ListViewItemComparer object. 
        ' THIS CODE SHOWS ONLY HOW TO SET THE SORT ICON!
        For i As Integer = 0 To lv.Columns.Count - 1
            If (i = e.Column) Then
                Select Case (so)
                    Case System.Windows.Forms.SortOrder.Ascending
                        lv.Columns(i).ImageKey = "up"
                        so = System.Windows.Forms.SortOrder.Descending
                    Case System.Windows.Forms.SortOrder.Descending
                        lv.Columns(i).ImageKey = "down"
                        so = System.Windows.Forms.SortOrder.Ascending
                    Case Else
                        lv.Columns(i).ImageKey = "empty"
                        so = System.Windows.Forms.SortOrder.None
                End Select
            Else
                lv.Columns(i).ImageKey = "empty"
            End If
        Next i
    End Sub

End Class

输出如下所示:

在此处输入图像描述

于 2013-09-07T19:33:29.027 回答