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
|
Public Class Form1
'tableau animals du zoo de vincennes
Private animals() As String
'Pour performance memoire, declare ici
'un Brush utilise pour dessiner dansCombo event DrawItem du .
Private ComboDefaultBrush As SolidBrush
Private ComboDefaultBrushBackColor As SolidBrush
'Idem pour le Font que je veux personaliser
Private ComboDefaultFont As Font
Public Sub New()
' Cet appel est requis par le Concepteur Windows Form.
InitializeComponent()
' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
' ForeColor et Font par defaut de notre combo exemple.
Me.ComboBox1.Font = New Font("Arial", 14, FontStyle.Italic)
Me.ComboBox1.ForeColor = Color.Black
'Mettre DrawMode.OwnerDrawFixed
Me.ComboBox1.DrawMode = DrawMode.OwnerDrawFixed
'Mettre ComboBoxStyle.DropDown
Me.ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
'notre tableau animals
animals = New String() {"Elephant", "c r o c o d i l e", "lion"}
ComboBox1.DataSource = animals
Me.Controls.Add(Me.ComboBox1)
End Sub
' On doit intercepter l'evenement DrawItem .
' Il sert à redefinir color et font d'un item lors du dessin.
Private Sub ComboBox1_DrawItem(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DrawItemEventArgs) _
Handles ComboBox1.DrawItem
'ici ton brush,font et brush de focus Aqua
Dim myBrushSel As Brush
Dim myFontSel As Font
Dim brushBackGround As Brush
' recupere le default brush using DrawItemEventArgs
If ComboDefaultBrush Is Nothing Then
ComboDefaultBrush = New SolidBrush(e.ForeColor)
Else
If Not ComboDefaultBrush.Color.Equals(e.ForeColor) Then
' ForeColor du combo a change , aussi dispose du brush &
' cree un nouveau.
ComboDefaultBrush.Dispose()
ComboDefaultBrush = New SolidBrush(e.ForeColor)
End If
End If
' recupere le default font using DrawItemEventArgs
If ComboDefaultFont Is Nothing Then
ComboDefaultFont = New Font(e.Font.FontFamily, e.Font.Size, e.Font.Style)
Else
If Not ComboDefaultFont.FontFamily.Equals(e.Font.FontFamily) Then
' Font du combo a change , aussi dispose du font &
' cree un nouveau.
ComboDefaultFont.Dispose()
ComboDefaultFont = New Font(e.Font.FontFamily, e.Font.Size, e.Font.Style)
End If
End If
' Selectionne le brush approprie suivant l'etat Selected de l' item
' attention c'est enum de bit-flag .You can't use "==" to compare them.
If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
myBrushSel = New SolidBrush(Color.Red)
myFontSel = New Font("Times New Roman", 18, FontStyle.Bold)
brushBackGround = New SolidBrush(Color.BlanchedAlmond)
Else
myBrushSel = ComboDefaultBrush
myFontSel = ComboDefaultFont
brushBackGround = New SolidBrush(e.BackColor)
End If
' obligatoire:Draw the background of item :.
e.DrawBackground()
' Dessine un rectangle Aqua sous l'tem
e.Graphics.FillRectangle(brushBackGround, e.Bounds)
' Draw each string in the array "par dessus" en rouge
e.Graphics.DrawString(Font.Name, myFontSel, myBrushSel, e.Bounds.X, e.Bounds.Y)
' obligatoire: Draw the focus rectangle if the mouse hovers over an item.
e.DrawFocusRectangle()
End Sub |
Partager