Public Class frmListViewPic
Dim WithEvents lv As New ListView With _
{.Parent = Me, .Width = 400, _
.OwnerDraw = True, .View = View.Details}
Dim iml As New ImageList
Dim displayed_image() As Bitmap 'angezeigte Bitmaps
Dim displayed_width As Integer
Private Sub frmListViewPic_Load(sender As System.Object, _
e As System.EventArgs) Handles MyBase.Load
'Imagelist mit Bildern füllen
iml.ImageSize = New Size(160, 90)
iml.Images.Add("KS", New Bitmap("C:\daten\ks.jpg"))
iml.Images.Add("EW", New Bitmap("C:\daten\ew.jpg"))
SetHeight(lv, 90) 'Höhe der ListvieItems einrichten
'Listview mit Items und Daten füllen
For i As Integer = 1 To 4
lv.Columns.Add("SP" & i.ToString)
Next i
For i As Integer = 1 To 6
Dim lvi As ListViewItem = lv.Items.Add("Item" & CStr(i))
lvi.SubItems.AddRange _
({"It" & CStr(i) & "S1", "It" & CStr(i) & "S2"})
lvi.SubItems.Add((i Mod 2).ToString)
Next i
Array.Resize(displayed_image, iml.images.Count)
lv.Refresh()
End Sub
Private Sub lv_DrawColumnHeader(sender As Object, _
e As System.Windows.Forms.DrawListViewColumnHeaderEventArgs) _
Handles lv.DrawColumnHeader
'Spaltenköpfe vom System zeichnen lassen
e.DrawDefault = True
End Sub
Private Sub lv_DrawSubItem(sender As Object, _
e As System.Windows.Forms.DrawListViewSubItemEventArgs) _
Handles lv.DrawSubItem
If e.ColumnIndex <> 3 Then
'Items vom System zeichnen lassen
e.DrawDefault = True
Else
'Subitem 3: Bild aus Imagelist anzeigen
With e.SubItem
'Bei geänderter Spaltenbreite:
'angezeigte Bilder neu skalieren
If Not .Bounds.Width = displayed_width Then
RescaleImages(.Bounds.Width, .Bounds.Height)
End If
Dim index As Integer = CInt(.Text) 'anzuzeigendes Bild
If displayed_image(index) IsNot Nothing Then
Dim w As Integer = displayed_image(index).Width
Dim h As Integer = displayed_image(index).Height
'Bild in Bounds des Listview-Items zentrieren
e.Graphics.DrawImage(displayed_image(index), _
.Bounds.Left + (.Bounds.Width - w) \ 2, _
.Bounds.Top + (.Bounds.Height - h) \ 2)
End If
End With
End If
End Sub
Private Sub RescaleImages(ByVal Itemwidth As Integer, ItemHeight As Integer)
For i As Integer = 0 To iml.Images.Count - 1
If displayed_image(i) IsNot Nothing Then _
displayed_image(i).Dispose()
If Itemwidth < 4 Then
displayed_image(i) = Nothing
Else
Using bmp As New Bitmap(iml.Images(i))
displayed_image(i) = GetResizedPic(bmp, Itemwidth, _
ItemHeight)
End Using
End If
Next i
displayed_width = Itemwidth
End Sub
Private Function GetResizedPic(ByVal bmp As Bitmap, _
ByVal ItemWidth As Integer, _
ByVal Itemheight As Integer) As Bitmap
If ItemWidth < 4 Then Return Nothing
Dim w As Integer = CInt(Math.Floor(bmp.Width * Itemheight / bmp.Height))
Dim h As Integer = Itemheight
If w > ItemWidth Then
w = ItemWidth
h = CInt(Math.Floor(bmp.Height * ItemWidth / bmp.Width))
End If
Return New Bitmap(bmp, New Size(w, h))
End Function
Private Sub SetHeight(lv As ListView, height As Integer)
'Hilfsfunktion: Höhe der ListviewItems
Dim imgList As New ImageList()
imgList.ImageSize = New Size(1, height)
lv.SmallImageList = imgList
End Sub
End Class |