Center subitem Icon in ListView

帅比萌擦擦* 提交于 2019-12-01 13:42:41

I am going to answer my own question here :)

Public Class ListViewSubIcons : Inherits System.Windows.Forms.ListView

    Public Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
    Public Const LVW_FIRST As Integer = &H1000
    Public Const LVM_GETEXTENDEDLISTVIEWSTYLE As Integer = LVW_FIRST + 54

    Public Const LVS_EX_GRIDLINES As Integer = &H1
    Public Const LVS_EX_SUBITEMIMAGES As Integer = &H2
    Public Const LVS_EX_CHECKBOXES As Integer = &H4
    Public Const LVS_EX_TRACKSELECT As Integer = &H8
    Public Const LVS_EX_FULLROWSELECT As Integer = &H20 ' applies to report mode only

    Private Const WM_NOTIFY As Integer = &H4E

    Public Enum ListStatus
        OK
        Fail
        Upgrade
    End Enum

    Private _liststatus As ListStatus = ListStatus.OK
    Public WriteOnly Property SetListStatus() As ListStatus
        Set(ByVal value As ListStatus)
            _liststatus = value
        End Set
    End Property


    ' Change the style to accept images on subitems.
    Public Sub New()

        ' This call is required by the Windows.Forms Form Designer.
        InitializeComponent()

        AddHandler Me.HandleCreated, AddressOf ListViewWithIcons_HandleCreated
        AddHandler Me.DrawSubItem, AddressOf lvResult_DrawSubItem
        AddHandler Me.DrawColumnHeader, AddressOf lvResult_DrawColumnHeader

        Me.OwnerDraw = True

    End Sub

    Private Sub ListViewWithIcons_HandleCreated(ByVal sender As Object, ByVal e As EventArgs)
        ' Change the style of listview to accept image on subitems
        Dim m As System.Windows.Forms.Message = New Message
        m.HWnd = Me.Handle
        m.Msg = LVM_GETEXTENDEDLISTVIEWSTYLE
        m.LParam = New IntPtr(LVS_EX_GRIDLINES Or LVS_EX_FULLROWSELECT Or LVS_EX_SUBITEMIMAGES Or LVS_EX_CHECKBOXES Or LVS_EX_TRACKSELECT)
        m.WParam = IntPtr.Zero
        Me.WndProc(m)
    End Sub

    ' Handle DrawSubItem event
    Private Sub lvResult_DrawSubItem(ByVal sender As Object, ByVal e As         DrawListViewSubItemEventArgs)

        'Only subitems with just icon's will have no text
        If e.SubItem.Text = "" Then
            Dim xpos = e.SubItem.Bounds.Location.X + (e.SubItem.Bounds.Width / 2) - 8
            Dim p As New PointF(xpos, e.SubItem.Bounds.Location.Y)
            e.DrawBackground()
            Select Case _liststatus
                Case ListStatus.OK
                    e.Graphics.DrawImage(My.Resources.Symbol_Check, p)
                Case ListStatus.Fail
                    e.Graphics.DrawImage(My.Resources.Delete, p)
                Case ListStatus.Upgrade
                    e.Graphics.DrawImage(My.Resources.Component1, p)
            End Select
        Else
            e.DrawDefault = True
        End If

    End Sub

    ' Handle DrawColumnHeader event
    Private Sub lvResult_DrawColumnHeader(ByVal sender As Object, ByVal e As DrawListViewColumnHeaderEventArgs)
        e.DrawDefault = True
        e.DrawBackground()
        e.DrawText()
    End Sub

End Class
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!