' See
' http://www.codeproject.com/KB/list/ListViewWithIcons.aspx.
Public Class ListViewWithIcons
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 Declare Function SendMessage Lib "user32.dll" _
Alias "SendMessageA" (ByVal hWnd As IntPtr, ByVal _
Msg As Int32, ByVal wParam As Int32, ByRef lParam _
As LV_ITEM) As Boolean
Public Structure LV_ITEM
Public mask As UInt32
Public iItem As Int32
Public iSubItem As Int32
Public state As UInt32
Public stateMask As UInt32
Public pszText As String
Public cchTextMax As Int32
Public iImage As Int32
Public lParam As IntPtr
End Structure
Public Const LVM_FIRST As Int32 = &H1000
Public Const LVM_GETITEM As Int32 = LVM_FIRST + 5
Public Const LVM_SETITEM As Int32 = LVM_FIRST + 6
Public Const LVIF_TEXT As Int32 = &H1
Public Const LVIF_IMAGE As Int32 = &H2
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_HEADERDRAGDROP As Integer = &H10
Public Const LVS_EX_FULLROWSELECT As Integer = &H20 '
' applies to report mode only
Public Const LVS_EX_ONECLICKACTIVATE As Integer = &H40
' Change the style to accept images on subitems.
Public Sub New()
' In .NET Framework 2.0 and later this
' must be done after the handle is created.
AddHandler Me.HandleCreated, AddressOf _
ListViewWithIcons_HandleCreated
End Sub
' Needed for .NET Framework 2.0 and later.
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
' Add an icon to a subitem.
Public Sub AddIconToSubitem(ByVal row As Integer, ByVal _
col As Integer, ByVal icon_num As Integer)
Dim lvi As New ListViewWithIcons.LV_ITEM()
lvi.iItem = row ' Row.
lvi.iSubItem = col ' Column.
' lvi.pszText = "Test" ' Text.
' Indicate what we're setting.
' lvi.mask = ListViewWithIcons.LVIF_IMAGE Or
' ListViewWithIcons.LVIF_TEXT
lvi.mask = ListViewWithIcons.LVIF_IMAGE
lvi.iImage = icon_num ' Image index in the
' ImageList.
' Send the LVM_SETITEM message.
ListViewWithIcons.SendMessage(Me.Handle, _
ListViewWithIcons.LVM_SETITEM, 0, lvi)
End Sub
End Class
|
Public Class Form1
Private lvwBooks As ListViewWithIcons
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
' Make the ListViewWithIcons.
lvwBooks = New ListViewWithIcons()
Me.Controls.Add(lvwBooks)
lvwBooks.SmallImageList = imlIcons
lvwBooks.Dock = DockStyle.Fill
lvwBooks.View = View.Details
' Make the column headers.
ListViewMakeColumnHeaders(lvwBooks, _
"Title", 230, HorizontalAlignment.Left, _
"URL", 220, HorizontalAlignment.Left, _
"ISBN", 130, HorizontalAlignment.Left, _
"Picture", 230, HorizontalAlignment.Left, _
"Pages", 50, HorizontalAlignment.Right, _
"Year", 60, HorizontalAlignment.Right)
' Add data rows.
ListViewMakeRow(lvwBooks, "Ready-to-Run Visual " & _
"Basic Algorithms", _
"http://www.vb-helper.com/vba.htm", _
"0-471-24268-3", _
"http://www.vb-helper.com/vba.jpg", "395", _
"1998")
ListViewMakeRow(lvwBooks, "Visual Basic Graphics " & _
"Programming", _
"http://www.vb-helper.com/vbgp.htm", _
"0-472-35599-2", _
"http://www.vb-helper.com/vbgp.jpg", "712", _
"2000")
ListViewMakeRow(lvwBooks, "Advanced Visual Basic " & _
"Techniques", _
"http://www.vb-helper.com/avbt.htm", _
"0-471-18881-6", _
"http://www.vb-helper.com/avbt.jpg", "440", _
"1997")
ListViewMakeRow(lvwBooks, "Custom Controls " & _
"Library", "http://www.vb-helper.com/ccl.htm", _
"0-471-24267-5", _
"http://www.vb-helper.com/ccl.jpg", "684", _
"1998")
ListViewMakeRow(lvwBooks, "Ready-to-Run Delphi " & _
"Algorithms", _
"http://www.vb-helper.com/da.htm", _
"0-471-25400-2", _
"http://www.vb-helper.com/da.jpg", "398", _
"1998")
ListViewMakeRow(lvwBooks, "Bug Proofing Visual " & _
"Basic", "http://www.vb-helper.com/err.htm", _
"0-471-32351-9", _
"http://www.vb-helper.com/err.jpg", "397", _
"1999")
ListViewMakeRow(lvwBooks, "Ready-to-Run Visual " & _
"Basic Code Library", _
"http://www.vb-helper.com/vbcl.htm", _
"0-471-33345-X", _
"http://www.vb-helper.com/vbcl.jpg", "424", _
"1999")
For r As Integer = 0 To lvwBooks.Items.Count - 1
For c As Integer = 0 To lvwBooks.Columns.Count _
- 1
lvwBooks.AddIconToSubitem(r, c, c)
Next c
Next r
End Sub
' Make the ListView's column headers.
' The ParamArray entries should alternate between
' strings and HorizontalAlignment values.
Private Sub ListViewMakeColumnHeaders(ByVal lvw As _
ListView, ByVal ParamArray header_info() As Object)
' Remove any existing headers.
lvw.Columns.Clear()
' Make the column headers.
For i As Integer = header_info.GetLowerBound(0) To _
header_info.GetUpperBound(0) Step 3
lvw.Columns.Add( _
DirectCast(header_info(i), String), _
DirectCast(header_info(i + 1), Integer), _
DirectCast(header_info(i + 2), _
HorizontalAlignment))
Next i
End Sub
' Make a ListView row.
Private Sub ListViewMakeRow(ByVal lvw As ListView, _
ByVal item_title As String, ByVal ParamArray _
subitem_titles() As String)
' Make the item.
Dim new_item As ListViewItem = _
lvw.Items.Add(item_title, 1)
' Make the sub-items.
For i As Integer = subitem_titles.GetLowerBound(0) _
To subitem_titles.GetUpperBound(0)
new_item.SubItems.Add(subitem_titles(i))
Next i
End Sub
End Class
|