Hi, I need to customize a Listview, so I'm drawing all the items myself.
Each item contains an image with a height of 50 pixels. I also need to add a CheckBox to each item. Because the Listview is ownerdrawn, I need to do all the CheckBox stuff myself too. I've done some custom ListView drawing before, but never had to draw CheckBoxes before.
Drawing the CheckBoxes is easy, but I have no idea how to capture mouseclick/hover and draw the correct CheckBox style.
1) How do I capture CheckBox mousehover to draw the CheckedHot/UnCheckedHot styles?
2) How do I capture CheckBox mouseclicks to draw the CheckedNormal/UnCheckedNormal styles?
Each item contains an image with a height of 50 pixels. I also need to add a CheckBox to each item. Because the Listview is ownerdrawn, I need to do all the CheckBox stuff myself too. I've done some custom ListView drawing before, but never had to draw CheckBoxes before.
Drawing the CheckBoxes is easy, but I have no idea how to capture mouseclick/hover and draw the correct CheckBox style.
1) How do I capture CheckBox mousehover to draw the CheckedHot/UnCheckedHot styles?
2) How do I capture CheckBox mouseclicks to draw the CheckedNormal/UnCheckedNormal styles?
vb.net Code:
Public Class ListViewExtended Inherits ListView '// Simply used to change the height of the rows Private imgList As New ImageList Public Sub New() Me.OwnerDraw = True Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, True) Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True) Me.View = View.Details Me.FullRowSelect = True Me.TabStop = False Me.Columns.Add("Items", 175, HorizontalAlignment.Left) imgList.ImageSize = New Size(50, 50) Me.SmallImageList = imgList End Sub Private Sub Me_DrawColumnHeader(ByVal sender As System.Object, _ ByVal e As System.Windows.Forms.DrawListViewColumnHeaderEventArgs) Handles Me.DrawColumnHeader e.DrawDefault = True End Sub Private Sub Me_DrawItem(ByVal sender As System.Object, _ ByVal e As System.Windows.Forms.DrawListViewItemEventArgs) Handles Me.DrawItem e.DrawDefault = False End Sub Private Sub Me_DrawSubItem(ByVal sender As Object, _ ByVal e As DrawListViewSubItemEventArgs) Handles Me.DrawSubItem Dim TextColor As Brush If (e.ItemState And ListViewItemStates.Selected) <> 0 Then e.Graphics.FillRectangle(SystemBrushes.Highlight, e.Bounds) '// Item is highlighted TextColor = Brushes.White Else If (e.ItemIndex Mod 2) <> 0 Then e.Graphics.FillRectangle(Brushes.White, e.Bounds) '// Odd Else e.Graphics.FillRectangle(Brushes.LightGray, e.Bounds) '// Even End If TextColor = Brushes.Black End If If Me.Items.Count > 0 Then '// ListViewItemExtended is a custom class stored in the Tag property of each item '// It contains the Image to draw and the CheckBox state Dim item = DirectCast(Me.Items(e.ItemIndex).Tag, ListViewItemExtended) e.Graphics.DrawImage(item.Image, e.Bounds.X + 30, e.Bounds.Y, 50, 50) If item.Checked Then CheckBoxRenderer.DrawCheckBox(e.Graphics, New Point(e.Bounds.X + 5, e.Bounds.Y + 10), VisualStyles.CheckBoxState.CheckedNormal) Else CheckBoxRenderer.DrawCheckBox(e.Graphics, New Point(e.Bounds.X + 5, e.Bounds.Y + 10), VisualStyles.CheckBoxState.UncheckedNormal) End If End If End Sub End Class