Heute möchten wir Ihnen eine ComboBox vorstellen, in der alle verfügbaren Schriften zur Auswahl angezeigt werden. Die jeweiligen Einträge sollen hierbei selbstverständlich in der jeweiligen Schriftart angezeigt werden! Das Füllen der ComboBox mit den verfügbaren Schriften erledigen wir im Form_Load Ereignis. Hier wird dem Control dann auch mitgeteilt, dass die Darstellung der Listen-Elemente nicht autom. erfolgen soll, sondern von uns selbst gezeichnet werden, was durch Setzen der DrawMode-Eigenschaft auf "OwnerDrawFixed" festgelegt wird. Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load ' alle verfügbaren Schriften auflisten With ComboBox1.Items For Each oFont As FontFamily In Font.FontFamily.Families .Add(oFont) Next End With ' Wir möchten uns um die Ausgabe der ' Listen-Einträge selbst kümmern ComboBox1.DrawMode = DrawMode.OwnerDrawFixed End Sub Wann immer jetzt ein Eintrag der ComboBox-Liste gezeichnet werden muss, löst das Control das DrawItem-Ereignis aus, d.h. hier muss unser Code zum Zeichnen des Eintrags eingefügt werden: Private Sub ComboBox1_DrawItem(ByVal sender As Object, _ ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem ' ComboBox-Eintrag (Schriftname) Dim sItem As String = ComboBox1.Items(e.Index).Name Dim oFont As Font ' Neues Schrift-Objekt erzeugen und verfügbare Styles berücksichtigen Dim oFamily As FontFamily = ComboBox1.Items(e.Index) If oFamily.IsStyleAvailable(FontStyle.Regular) Then oFont = New Font(sItem, ComboBox1.Font.Size, FontStyle.Regular) ElseIf oFamily.IsStyleAvailable(FontStyle.Italic) Then oFont = New Font(sItem, ComboBox1.Font.Size, FontStyle.Italic) ElseIf oFamily.IsStyleAvailable(FontStyle.Bold) Then oFont = New Font(sItem, ComboBox1.Font.Size, FontStyle.Bold) ElseIf oFamily.IsStyleAvailable(FontStyle.Underline) Then oFont = New Font(sItem, ComboBox1.Font.Size, FontStyle.Underline) Else oFont = New Font(sItem, ComboBox1.Font.Size, FontStyle.Strikeout) End If ' Brush für Textfarbe Dim oBrush As Brush = New SolidBrush(e.ForeColor) ' Hintergrund e.DrawBackground() ' Text (Schriftname) ausgeben e.Graphics.DrawString(sItem, oFont, oBrush, e.Bounds.X, e.Bounds.Y) ' Aufräumen oBrush.Dispose() End Sub |