C#中如何获取一个字体的宽度值(像素单位)
原帖地址:http://blog.csdn.net/hanghwp/archive/2009/09/04/4517387.aspx
1Graphics g = this.CreateGraphics();
2SizeF sizeF = g.MeasureString("A", new Font("宋体", 9));
3MessageBox.Show(sizeF.Width + " " + sizeF.Height);
4g.Dispose();
2SizeF sizeF = g.MeasureString("A", new Font("宋体", 9));
3MessageBox.Show(sizeF.Width + " " + sizeF.Height);
4g.Dispose();
1 private void MeasureText1(PaintEventArgs e)
2 {
3 String text1 = "Measure this text";
4 Font arialBold = new Font("Arial", 12.0F);
5 Size textSize = TextRenderer.MeasureText(text1, arialBold);
6 TextRenderer.DrawText(e.Graphics, text1, arialBold,
7 new Rectangle(new Point(10, 10), textSize), Color.Red);
8 }
9
2 {
3 String text1 = "Measure this text";
4 Font arialBold = new Font("Arial", 12.0F);
5 Size textSize = TextRenderer.MeasureText(text1, arialBold);
6 TextRenderer.DrawText(e.Graphics, text1, arialBold,
7 new Rectangle(new Point(10, 10), textSize), Color.Red);
8 }
9