Center subitem Icon in ListView

后端 未结 1 2056
生来不讨喜
生来不讨喜 2021-01-16 07:11

I would like to center the icon in one of my columns. The way I currently add icons is by calling the AddIconToSubitem method after the row has been created. How would I m

相关标签:
1条回答
  • 2021-01-16 07:31

    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
    
    0 讨论(0)
提交回复
热议问题