O
orekinbck
Hi There
I have wired up an event handler to a combo boxes DrawItem event, and I
want to draw a string that is vertically centered in the combo box.
At the moment I am doing it like this:
int centeredY = e.Bounds.Y + (int)((e.Bounds.Height -
mf.GetHeight(e.Graphics))/2);
Is there an easier way ? FYI, here is a more complete listing:
private void cmbFonts_DrawItem(object sender,
System.Windows.Forms.DrawItemEventArgs e)
{
if (e.Index == -1 || sender == null)
return;
ComboBox cboFont = (ComboBox)sender;
FontFamily ff = new FontFamily(cboFont.Items[e.Index].ToString());
Font mf = new Font(ff.Name,10,GetFontStyle(ff));
e.DrawBackground();
e.DrawFocusRectangle();
int centeredY = e.Bounds.Y + (int)((e.Bounds.Height -
mf.GetHeight(e.Graphics))/2);
e.Graphics.DrawString(ff.Name, mf,new
SolidBrush(e.ForeColor),e.Bounds.X,centeredY);
}
private FontStyle GetFontStyle(FontFamily ff)
{
FontStyle mfs = FontStyle.Regular;
if(!ff.IsStyleAvailable(mfs))
mfs = FontStyle.Italic;
if(!ff.IsStyleAvailable(mfs))
mfs = FontStyle.Bold;
return mfs;
}
Cheers
Bill
I have wired up an event handler to a combo boxes DrawItem event, and I
want to draw a string that is vertically centered in the combo box.
At the moment I am doing it like this:
int centeredY = e.Bounds.Y + (int)((e.Bounds.Height -
mf.GetHeight(e.Graphics))/2);
Is there an easier way ? FYI, here is a more complete listing:
private void cmbFonts_DrawItem(object sender,
System.Windows.Forms.DrawItemEventArgs e)
{
if (e.Index == -1 || sender == null)
return;
ComboBox cboFont = (ComboBox)sender;
FontFamily ff = new FontFamily(cboFont.Items[e.Index].ToString());
Font mf = new Font(ff.Name,10,GetFontStyle(ff));
e.DrawBackground();
e.DrawFocusRectangle();
int centeredY = e.Bounds.Y + (int)((e.Bounds.Height -
mf.GetHeight(e.Graphics))/2);
e.Graphics.DrawString(ff.Name, mf,new
SolidBrush(e.ForeColor),e.Bounds.X,centeredY);
}
private FontStyle GetFontStyle(FontFamily ff)
{
FontStyle mfs = FontStyle.Regular;
if(!ff.IsStyleAvailable(mfs))
mfs = FontStyle.Italic;
if(!ff.IsStyleAvailable(mfs))
mfs = FontStyle.Bold;
return mfs;
}
Cheers
Bill