OwnerDrawn Combobox - Varying Fonts
- From: djdouma <djdouma@xxxxxxxxxx>
- Date: Tue, 12 Feb 2008 20:29:02 -0800 (PST)
I'm developing a combobox that lists the fonts in their given font
face, attempting to replicate how MS Word displays them. I have my
code mostly working, but the problem I'm running into is some fonts,
like 'Cambria Math', don't fit within the fixed ItemHeight (hardcoded
to 20 at the moment). I noticed that in Word, it reduces the font
size until it fits into the ItemHeight. I tried replicating this
without success (basically I reduced the font size by one point until
e.Graphics.MeasureString("Tunga", font).Height fit within
e.Bounds.Height). I could get the font size to shrink to fit within
the ItemHeight, but then I noticed that fonts like 'Tunga' were now
appearing smaller. (see code below)
Any ideas?
protected override void OnMeasureItem(MeasureItemEventArgs e)
{
Font font = ((ComboBoxFontItem)Items[e.Index]).Font;
SizeF stringSize = e.Graphics.MeasureString(font.Name,
font);
// Set the height and width of the item
//e.ItemHeight = (int)stringSize.Height;
e.ItemHeight = 20;
e.ItemWidth = (int)stringSize.Width;
}
protected override void
OnDrawItem(System.Windows.Forms.DrawItemEventArgs e)
{
Brush brush;
// Create the brush using the ForeColor specified by the
DrawItemEventArgs
if ( foreColorBrush == null )
foreColorBrush = new SolidBrush(e.ForeColor);
else if ( foreColorBrush.Color != e.ForeColor )
{
// The control's ForeColor has changed, so dispose of
the cached brush and
// create a new one.
foreColorBrush.Dispose();
foreColorBrush = new SolidBrush(e.ForeColor);
}
// Select the appropriate brush depending on if the item
is selected.
// Since State can be a combinateion (bit-flag) of enum
values, you can't use
// "==" to compare them.
if ( (e.State & DrawItemState.Selected) ==
DrawItemState.Selected )
brush = SystemBrushes.HighlightText;
else
brush = foreColorBrush;
if (Items[e.Index].ToString() == "Tunga")
{
}
else if (Items[e.Index].ToString() == "Mangal")
{
}
Font font = ((ComboBoxFontItem)Items[e.Index]).Font;
e.DrawBackground();
e.Graphics.DrawString(font.Name, font, brush, e.Bounds);
e.DrawFocusRectangle();
}
.
- Follow-Ups:
- Re: OwnerDrawn Combobox - Varying Fonts
- From: Martin M
- Re: OwnerDrawn Combobox - Varying Fonts
- From: RobinS
- Re: OwnerDrawn Combobox - Varying Fonts
- Prev by Date: WebBrowser Control Print and Preview Headers and Footers
- Next by Date: Re: OwnerDrawn Combobox - Varying Fonts
- Previous by thread: WebBrowser Control Print and Preview Headers and Footers
- Next by thread: Re: OwnerDrawn Combobox - Varying Fonts
- Index(es):
Relevant Pages
|