' Derive an implementation of the
' OwnerDrawnListBox class
Class FontListBox
Inherits OwnerDrawnListBox
Private Const FONT_SIZE As Integer = 10
Private Const DRAW_OFFSET As Integer = 5
Public Sub New()
' Determine what the item height should be
' by adding 30% padding after measuring
' the letter A with the selected font.
Dim g As Graphics = Me.CreateGraphics()
Me.ItemHeight = Fix(g.MeasureString("A", Me.Font).Height * 1.3)
g.Dispose()
End Sub
' Return the name of the selected font.
Public ReadOnly Property SelectedFaceName() As String
Get
Return CStr(Me.Items(Me.SelectedIndex))
End Get
End Property
' Determine what the text color should be
' for the selected item drawn as highlighted
Function CalcTextColor(ByVal backgroundColor As Color) As Color
If backgroundColor.Equals(Color.Empty) Then
Return Color.Black
End If
Dim sum As Integer = backgroundColor.R + backgroundColor.G + backgroundColor.B
If sum > 256 Then
Return Color.Black
Else
Return Color.White
End If
End Function
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
Dim font As Font
Dim fontColor As Color
' The base class contains a bitmap, offScreen, for constructing
' the control and is rendered when all items are populated.
' This technique prevents flicker.
Dim gOffScreen As Graphics = Graphics.FromImage(Me.OffScreen)
gOffScreen.FillRectangle(New SolidBrush(Me.BackColor), Me.ClientRectangle)
Dim itemTop As Integer = 0
' Draw the fonts in the list.
Dim n As Integer
For n = Me.VScrollBar.Value To (Me.VScrollBar.Value + DrawCount) - 1
' If the font name contains "dings" it needs to be displayed
' in the list box with a readable font with the default font.
If CStr(Me.Items(n)).ToLower().IndexOf("dings") <> -1 Then
font = New Font(Me.Font.Name, FONT_SIZE, FontStyle.Regular)
Else
font = New Font(CStr(Me.Items(n)), FONT_SIZE, FontStyle.Regular)
End If
' Draw the selected item to appear highlighted
If n = Me.SelectedIndex Then
gOffScreen.FillRectangle(New SolidBrush(SystemColors.Highlight), 1, itemTop + 1, Me.ClientSize.Width - IIf(Me.VScrollBar.Visible, Me.VScrollBar.Width, 2), Me.ItemHeight)
' If the scroll bar is visible, subtract the scrollbar width
' otherwise subtract 2 for the width of the rectangle
fontColor = CalcTextColor(SystemColors.Highlight)
Else
fontColor = Me.ForeColor
End If
' Draw the item
gOffScreen.DrawString(CStr(Me.Items(n)), font, New SolidBrush(fontColor), DRAW_OFFSET, itemTop)
itemTop += Me.ItemHeight
Next n
' Draw the list box
e.Graphics.DrawImage(Me.OffScreen, 1, 1)
gOffScreen.Dispose()
End Sub
' Draws the external border around the control.
Protected Overrides Sub OnPaintBackground(ByVal e As PaintEventArgs)
e.Graphics.DrawRectangle(New Pen(Color.Black), 0, 0, Me.ClientSize.Width - 1, Me.ClientSize.Height - 1)
End Sub
End Class
// Derive an implementation of the
// OwnerDrawnListBox class
class FontListBox : OwnerDrawnListBox
{
const int FONT_SIZE = 10;
const int DRAW_OFFSET = 5;
public FontListBox()
{
// Determine what the item height should be
// by adding 30% padding after measuring
// the letter A with the selected font.
Graphics g = this.CreateGraphics();
this.ItemHeight = (int)(g.MeasureString("A", this.Font).Height * 1.3);
g.Dispose();
}
// Return the name of the selected font.
public string SelectedFaceName
{
get
{
return (string)this.Items[this.SelectedIndex];
}
}
// Determine what the text color should be
// for the selected item drawn as highlighted
Color CalcTextColor(Color backgroundColor)
{
if(backgroundColor.Equals(Color.Empty))
return Color.Black;
int sum = backgroundColor.R + backgroundColor.G + backgroundColor.B;
if(sum > 256)
return Color.Black;
else
return Color.White;
}
protected override void OnPaint(PaintEventArgs e)
{
Font font;
Color fontColor;
// The base class contains a bitmap, offScreen, for constructing
// the control and is rendered when all items are populated.
// This technique prevents flicker.
Graphics gOffScreen = Graphics.FromImage(this.OffScreen);
gOffScreen.FillRectangle(new SolidBrush(this.BackColor), this.ClientRectangle);
int itemTop = 0;
// Draw the fonts in the list.
for(int n = this.VScrollBar.Value; n < this.VScrollBar.Value + DrawCount; n++)
{
// If the font name contains "dings" it needs to be displayed
// in the list box with a readable font with the default font.
if(((string)this.Items[n]).ToLower().IndexOf("dings") != -1)
font = new Font(this.Font.Name, FONT_SIZE, FontStyle.Regular);
else
font = new Font((string)this.Items[n], FONT_SIZE, FontStyle.Regular);
// Draw the selected item to appear highlighted
if(n == this.SelectedIndex)
{
gOffScreen.FillRectangle(new SolidBrush(SystemColors.Highlight),
1,
itemTop + 1,
// If the scroll bar is visible, subtract the scrollbar width
// otherwise subtract 2 for the width of the rectangle
this.ClientSize.Width - (this.VScrollBar.Visible ? this.VScrollBar.Width : 2),
this.ItemHeight);
fontColor = CalcTextColor(SystemColors.Highlight);
}
else
fontColor = this.ForeColor;
// Draw the item
gOffScreen.DrawString((string)this.Items[n], font, new SolidBrush(fontColor), DRAW_OFFSET, itemTop);
itemTop += this.ItemHeight;
}
// Draw the list box
e.Graphics.DrawImage(this.OffScreen, 1, 1);
gOffScreen.Dispose();
}
// Draws the external border around the control.
protected override void OnPaintBackground(PaintEventArgs e)
{
e.Graphics.DrawRectangle(new Pen(Color.Black), 0, 0, this.ClientSize.Width - 1, this.ClientSize.Height - 1);
}
}