Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27421

CheckBoxRenderer and ownerdrawn ListView

$
0
0
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?


vb.net Code:
  1. Public Class ListViewExtended
  2.     Inherits ListView
  3.  
  4.     '// Simply used to change the height of the rows
  5.     Private imgList As New ImageList
  6.  
  7.     Public Sub New()
  8.         Me.OwnerDraw = True
  9.         Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
  10.         Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
  11.         Me.View = View.Details
  12.         Me.FullRowSelect = True
  13.         Me.TabStop = False
  14.         Me.Columns.Add("Items", 175, HorizontalAlignment.Left)
  15.         imgList.ImageSize = New Size(50, 50)
  16.         Me.SmallImageList = imgList
  17.     End Sub
  18.  
  19.     Private Sub Me_DrawColumnHeader(ByVal sender As System.Object, _
  20.         ByVal e As System.Windows.Forms.DrawListViewColumnHeaderEventArgs) Handles Me.DrawColumnHeader
  21.         e.DrawDefault = True
  22.     End Sub
  23.  
  24.     Private Sub Me_DrawItem(ByVal sender As System.Object, _
  25.         ByVal e As System.Windows.Forms.DrawListViewItemEventArgs) Handles Me.DrawItem
  26.         e.DrawDefault = False
  27.     End Sub
  28.  
  29.     Private Sub Me_DrawSubItem(ByVal sender As Object, _
  30.         ByVal e As DrawListViewSubItemEventArgs) Handles Me.DrawSubItem
  31.  
  32.         Dim TextColor As Brush
  33.  
  34.         If (e.ItemState And ListViewItemStates.Selected) <> 0 Then
  35.             e.Graphics.FillRectangle(SystemBrushes.Highlight, e.Bounds) '// Item is highlighted
  36.             TextColor = Brushes.White
  37.         Else
  38.             If (e.ItemIndex Mod 2) <> 0 Then
  39.                 e.Graphics.FillRectangle(Brushes.White, e.Bounds) '// Odd
  40.             Else
  41.                 e.Graphics.FillRectangle(Brushes.LightGray, e.Bounds) '// Even
  42.             End If
  43.             TextColor = Brushes.Black
  44.         End If
  45.  
  46.         If Me.Items.Count > 0 Then
  47.             '// ListViewItemExtended is a custom class stored in the Tag property of each item
  48.             '// It contains the Image to draw and the CheckBox state
  49.             Dim item = DirectCast(Me.Items(e.ItemIndex).Tag, ListViewItemExtended)
  50.  
  51.             e.Graphics.DrawImage(item.Image, e.Bounds.X + 30, e.Bounds.Y, 50, 50)
  52.  
  53.             If item.Checked Then
  54.                 CheckBoxRenderer.DrawCheckBox(e.Graphics, New Point(e.Bounds.X + 5, e.Bounds.Y + 10), VisualStyles.CheckBoxState.CheckedNormal)
  55.             Else
  56.                 CheckBoxRenderer.DrawCheckBox(e.Graphics, New Point(e.Bounds.X + 5, e.Bounds.Y + 10), VisualStyles.CheckBoxState.UncheckedNormal)
  57.             End If
  58.         End If
  59.     End Sub
  60. End Class

Viewing all articles
Browse latest Browse all 27421

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>