1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
Public Class StyledListBox
Inherits ListBox
' Create a new strongly typed Items property so
' the Properties Window can provide an editor for it.
' Delegate it to the inherited Items property.
Public Shadows Property Items() As StyledItem()
Get
Dim itemArray(0 To MyBase.Items.Count - 1) As StyledItem
For i As Integer = 0 To MyBase.Items.Count - 1
itemArray(i) = DirectCast(MyBase.Items(i), StyledItem)
Next i
Return itemArray
End Get
Set(ByVal value() As StyledItem)
MyBase.Items.Clear()
For Each styledItem As StyledItem In value
MyBase.Items.Add(styledItem)
Next styledItem
End Set
End Property
'' Specify OwnerDraw mode.
Public Sub New()
MyBase.New()
InitializeComponent()
' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
MyBase.DrawMode = Windows.Forms.DrawMode.OwnerDrawVariable
End Sub
' Set the size for an item.
Private Const ITEM_MARGIN As Integer = 2
Private Sub StyledListBox_MeasureItem(ByVal sender As Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles Me.MeasureItem
If e.Index < 0 Or e.Index >= MyBase.Items.Count Then Exit Sub
Dim gr As Graphics = e.Graphics
' Convert the item into a StyledListBoxItem.
Dim the_item As StyledItem =
DirectCast(MyBase.Items(e.Index), StyledItem)
' Debug.WriteLine(Measure: & the_item.Text)
' See how much room it will need.
Dim h As Integer = 0
Dim w As Integer = 0
' Allow room for the image if present.
If the_item.Image IsNot Nothing Then
w = the_item.Image.Width + 2 * ITEM_MARGIN
h = the_item.Image.Height + 2 * ITEM_MARGIN
End If
' Measure the text.
If (the_item.Text IsNot Nothing) AndAlso (the_item.Text.Length > 0) Then
Dim fnt As Font = the_item.Font
If fnt Is Nothing Then fnt = Me.Parent.Font
Dim item_size As SizeF = gr.MeasureString(the_item.Text, fnt)
Dim txtHeight As Integer = CInt(item_size.Height + 2 * ITEM_MARGIN)
If txtHeight > h Then h = txtHeight
w += CInt(item_size.Width + 2 * ITEM_MARGIN)
End If
e.ItemWidth = w
e.ItemHeight = h
End Sub
' Draw the item.
Private Sub StyledListBox_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles Me.DrawItem
If e.Index < 0 Or e.Index >= MyBase.Items.Count Then Exit Sub
Dim gr As Graphics = e.Graphics
' Convert the item into a StyledListBoxItem.
Dim the_item As StyledItem =
DirectCast(MyBase.Items(e.Index), StyledItem)
' Debug.WriteLine(Draw: & the_item.Text)
' Clear the items drawing area.
e.DrawBackground()
If Me.SelectedIndex = e.Index Then e.DrawFocusRectangle()
' Draw the image if present.
Dim x As Integer = ITEM_MARGIN
If the_item.Image IsNot Nothing Then
Dim y As Integer =
e.Bounds.Top + (e.Bounds.Height - the_item.Image.Height) \ 2
gr.DrawImage(the_item.Image, x, y)
x = the_item.Image.Width + 2 * ITEM_MARGIN
End If
' Draw the text if present.
If (the_item.Text IsNot Nothing) AndAlso (the_item.Text.Length > 0) Then
Dim fnt As Font = the_item.Font
If fnt Is Nothing Then fnt = Me.Parent.Font
Dim item_size As SizeF =
gr.MeasureString(the_item.Text, fnt)
Dim y As Integer =
e.Bounds.Top + CInt((e.Bounds.Height - item_size.Height) / 2)
Using brush As New SolidBrush(the_item.Color)
e.Graphics.DrawString(the_item.Text, fnt, brush, x, y)
End Using
End If
End Sub
End Class |
Partager