我想把一个字串,自适应打印到一个固定的大小之中,不许换行,寻求帮助
例如:
字串"ABCDabcd1234"
打印到一个高度20, 宽度 50 ,必须自适应这个大小,也就是高度达到20,宽度达到50,并且不换行或者只适应一项。例如宽度限制在50,高度比20小;或者高度20,宽度比50小如果有不理解的地方,请留言
例如:
字串"ABCDabcd1234"
打印到一个高度20, 宽度 50 ,必须自适应这个大小,也就是高度达到20,宽度达到50,并且不换行或者只适应一项。例如宽度限制在50,高度比20小;或者高度20,宽度比50小如果有不理解的地方,请留言
_hF = new Font("Times New Roman", (float)mHeight / (float)1.77069759, FontStyle.Regular);
这里(float)mHeight / (float)1.77069759算出字符在这个高度下的字号
{Width = 14.9536133 Height = 17.7069759}
Height: 17.7069759
IsEmpty: false
Width: 14.9536133 A字符的宽度
mFontSize
{Width = 10.972765 Height = 17.7069759}
Height: 17.7069759
IsEmpty: false
Width: 10.972765 a字符的宽度
mFontSize
{Width = 13.3666992 Height = 17.7069759}
Height: 17.7069759
IsEmpty: false
Width: 13.3666992 L字符的宽度
mFontSize
{Width = 8.599175 Height = 17.7069759}
Height: 17.7069759
IsEmpty: false
Width: 8.599175 l字符的宽度(字符l)
mFontSize
{Width = 11.7797852 Height = 17.7069759}
Height: 17.7069759
IsEmpty: false
Width: 11.7797852 1字符的宽度(数字1)
private void panel1_Paint(object sender, PaintEventArgs e)
{
string text = "MessageBox";
SizeF textSize=e.Graphics.MeasureString(text, this.Font);
Size areaSize = this.panel1.Size;
float hScale = areaSize.Width / textSize.Width;
float vScale = areaSize.Height / textSize.Height;
float scale = hScale > vScale ? vScale : hScale;
e.Graphics.ScaleTransform(scale, scale);
e.Graphics.DrawString(text, this.Font, Brushes.Red, this.panel1.ClientRectangle);
}