//先拖一个label1到界面,然后在Paint事件里面复制代码
private void Form1_Paint(object sender, PaintEventArgs e)
{
Font f = label1.Font;
Graphics g = e.Graphics;
SizeF z = g.MeasureString(label1.Text, f);//关键
//为了验证是否为显示的字符串长度
g.DrawRectangle(new Pen(Color.Red), 0, 0, z.Width, z.Height);
}
http://msdn.microsoft.com/en-us/library/6xe5hazb.aspx
直接获取字符串的Length属性,然后乘你的单个文字尺寸,就能得到矩形的宽度了啊。
就是获取字符串的长度和高度
SizeF sizef = e.Graphics.MeasureString(字符串,字体);