Live Chat Icon For mobile
Live Chat Icon

WinForms FAQ

Find answers for the most frequently asked questions
Expand All Collapse All

Try calling one of the overloads of MeasureString that takes an StringFormat parameter and pass it StringFormat.GenericTypographic. Here is some code that shows the same string and font giving different results depending upon the StringFormat parameter.

StringFormat sf = new  StringFormat(StringFormat.GenericTypographic);

SizeF size =  e.Graphics.MeasureString('this has some words', Font, 500, sf);
SizeF size1 = e.Graphics.MeasureString('this has some words', Font);
string s = string.Format('GenericTypographic={0:f2}   GenericDefault={1:f2}', size.Width, size1.Width);
MessageBox.Show(s);

Permalink

James DeBroeck of Microsoft gave this response on the microsoft.public.dotnet.framework.windowsforms.

Here is some C# OnPaint code that will show you how to use FontFamily to get a list of fonts:

protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
    int i, nCount;
    SolidBrush  b = new SolidBrush( Color.Black );
    int x = 0, y = 0;

    nCount = FontFamily.Families.Length;
    
    for(i=0;i 0)
	StylesDesc = StylesDesc.Substring(0,StylesDesc.Length-1);

        f = new Font( FontFamily.Families[i].Name, 12, fs);

        String s = FontFamily.Families[i].Name + ' ' + StylesDesc;
        e.Graphics.DrawString( s, f, b, x, y );
        y += f.Height;

    }
}
Permalink

The key is that you have to calculate the ascent height of the font. The font ascent as reported by FontFamily.GetCellAscent is in what is called ’Design Units’. The Cell Spacing design unit value of fonts is proportional to the actual height of the font on the device. We use this relationship to calculate cell ascent in device units.

The rendering code has to just ensure that the x, y position passed to DrawString takes care of the ascent.

	private void HandlePaint(object sender, PaintEventArgs args)
	{
		// clear the background
		Graphics g = args.Graphics;
		g.Clear(Color.AliceBlue);
		
		// create a pen
		Pen pen = new Pen(Color.Red, 1f);
		
		// the string to be drawn
		string s = 'Side by side';
		
		// the first font
		Font f1 = new Font('Arial', 10f);
		float strWidth1 = g.MeasureString(s, f1).Width;
		float fontHeight1 = f1.GetHeight(g);	
		float fontAscentHeight1 = (fontHeight1/f1.FontFamily.GetLineSpacing(f1.Style))*f1.FontFamily.GetCellAscent(f1.Style);
			
		// the second font
		Font f2 = new Font('Times New Roman', 48);
			float fontHeight2 = f2.GetHeight(g);	
			float fontAscentHeight2 = (fontHeight2/f2.FontFamily.GetLineSpacing(f2.Style))*f2.FontFamily.GetCellAscent(f2.Style);

		// draw the base line	
		Point ptStart = new Point(0, this.ClientSize.Height/2);
		Point ptEnd = new Point(this.ClientSize.Width, this.ClientSize.Height/2);
		g.DrawLine(Pens.Black, ptStart, ptEnd);
	
		// draw string with first font
		g.DrawString(s, f1, Brushes.Red, new PointF(0, ptStart.Y - fontAscentHeight1));
		// draw string with second font
		g.DrawString(s, f2, Brushes.Red, new PointF(strWidth1, ptStart.Y - fontAscentHeight2));
	}
Permalink

Share with

Share on twitter
Share on facebook
Share on linkedin

Couldn't find the FAQs you're looking for?

Please submit your question and answer.