OwnerDrawn Combobox - Varying Fonts



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();
}
.



Relevant Pages

  • Re: OwnerDrawn Combobox - Varying Fonts
    ... don't fit within the fixed ItemHeight (hardcoded ... it reduces the font ... Brush brush; ... foreColorBrush = new SolidBrush; ...
    (microsoft.public.dotnet.framework.windowsforms.controls)
  • Re: Panel mit Hintergundgrafik 1024x768 und ca. 50 Controls Labels performance Problem
    ... DrawString in eine geeignete Klasse, ... PseudoLabel lbl = new PseudoLabel; ... in Dispose die PseudoLabel freigeben (Brush!) ... private Font font; ...
    (microsoft.public.de.german.entwickler.dotnet.csharp)
  • Re: Printing the contents of a database
    ... You might try using the DrawString override as ... I'd also move the Font, Brush, and Pen declarations outside the ... >> Paraschiv, ...
    (microsoft.public.dotnet.framework.drawing)
  • =?iso-8859-1?q?Re:_Datagridview_Spalten=FCberschriften_vertikal_anzeigen?=
    ... ich möchte in einem Datagridview die Spaltenüberschriften vertikal ... Font font = new Font("Microsoft Sans Serif", 10F, FontStyle.Regular, ... Brush schwarz = new SolidBrush; ... e.Graphics.TranslateTransform(6, chHeight - 6); ...
    (microsoft.public.de.german.entwickler.dotnet.csharp)
  • Re: OwnerDrawn Combobox - Varying Fonts
    ... Define the font in which you are going to draw your font name and include the font size set for the combobox. ... e.Graphics.DrawString(fontName, thisFont, brush, pf); ... don't fit within the fixed ItemHeight (hardcoded ... foreColorBrush = new SolidBrush; ...
    (microsoft.public.dotnet.framework.windowsforms.controls)