Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Drawing
Imports System.Data
Imports System.Windows.Forms
Namespace FontCombo
Public Class FontComboBox
Inherits ComboBox
Public Sub New()
MaxDropDownItems = 20
IntegralHeight = False
Sorted = False
DropDownStyle = ComboBoxStyle.DropDownList
DrawMode = DrawMode.OwnerDrawVariable
End Sub 'New
Public Sub Populate(b As Boolean)
both = b
Dim ff As FontFamily
For Each ff In FontFamily.Families
If ff.IsStyleAvailable(FontStyle.Regular) Then
Items.Add(ff.Name)
End If
Next ff
If Items.Count > 0 Then
SelectedIndex = 0
End If
ttimg = New Bitmap([GetType](), "ttfbmp.bmp")
End Sub 'Populate
Protected Overrides Sub OnMeasureItem(e As System.Windows.Forms.MeasureItemEventArgs)
If e.Index > - 1 Then
Dim w As Integer = 0
Dim fontstring As String = Items(e.Index).ToString()
Dim g As Graphics = CreateGraphics()
e.ItemHeight = CInt(g.MeasureString(fontstring, New Font(fontstring, 10)).Height)
w = CInt(g.MeasureString(fontstring, New Font(fontstring, 10)).Width)
If both Then
Dim h1 As Integer = CInt(g.MeasureString(samplestr, New Font(fontstring, 10)).Height)
Dim h2 As Integer = CInt(g.MeasureString(Items(e.Index).ToString(), New Font("Arial", 10)).Height)
Dim w1 As Integer = CInt(g.MeasureString(samplestr, New Font(fontstring, 10)).Width)
Dim w2 As Integer = CInt(g.MeasureString(Items(e.Index).ToString(), New Font("Arial", 10)).Width)
If h1 > h2 Then
h2 = h1
End If
e.ItemHeight = h2
w = w1 + w2
End If
w += ttimg.Width * 2
If w > maxwid Then
maxwid = w
End If
If e.ItemHeight > 20 Then
e.ItemHeight = 20
End If
End If
MyBase.OnMeasureItem(e)
End Sub 'OnMeasureItem
Protected Overrides Sub OnDrawItem(e As System.Windows.Forms.DrawItemEventArgs)
If e.Index > - 1 Then
Dim fontstring As String = Items(e.Index).ToString()
nfont = New Font(fontstring, 10)
Dim afont As New Font("Arial", 10)
If both Then
Dim g As Graphics = CreateGraphics()
Dim w As Integer = CInt(g.MeasureString(fontstring, afont).Width)
If(e.State And DrawItemState.Focus) = 0 Then
e.Graphics.FillRectangle(New SolidBrush(SystemColors.Window), e.Bounds.X + ttimg.Width, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height)
e.Graphics.DrawString(fontstring, afont, New SolidBrush(SystemColors.WindowText), e.Bounds.X + ttimg.Width * 2, e.Bounds.Y)
e.Graphics.DrawString(samplestr, nfont, New SolidBrush(SystemColors.WindowText), e.Bounds.X + w + ttimg.Width * 2, e.Bounds.Y)
Else
e.Graphics.FillRectangle(New SolidBrush(SystemColors.Highlight), e.Bounds.X + ttimg.Width, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height)
e.Graphics.DrawString(fontstring, afont, New SolidBrush(SystemColors.HighlightText), e.Bounds.X + ttimg.Width * 2, e.Bounds.Y)
e.Graphics.DrawString(samplestr, nfont, New SolidBrush(SystemColors.HighlightText), e.Bounds.X + w + ttimg.Width * 2, e.Bounds.Y)
End If
Else
If(e.State And DrawItemState.Focus) = 0 Then
e.Graphics.FillRectangle(New SolidBrush(SystemColors.Window), e.Bounds.X + ttimg.Width, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height)
e.Graphics.DrawString(fontstring, nfont, New SolidBrush(SystemColors.WindowText), e.Bounds.X + ttimg.Width * 2, e.Bounds.Y)
Else
e.Graphics.FillRectangle(New SolidBrush(SystemColors.Highlight), e.Bounds.X + ttimg.Width, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height)
e.Graphics.DrawString(fontstring, nfont, New SolidBrush(SystemColors.HighlightText), e.Bounds.X + ttimg.Width * 2, e.Bounds.Y)
End If
End If
e.Graphics.DrawImage(ttimg, New Point(e.Bounds.X, e.Bounds.Y))
End If
MyBase.OnDrawItem(e)
End Sub 'OnDrawItem
Private nfont As Font
Private both As Boolean = False
Private maxwid As Integer = 0
Private samplestr As String = " - Hello World"
Private ttimg As Image
Protected Overrides Sub OnDropDown(e As System.EventArgs)
Me.DropDownWidth = maxwid + 30
End Sub 'OnDropDown
End Class 'FontComboBox
End Namespace 'FontCombo