【发布时间】:2019-07-17 21:38:25
【问题描述】:
标签中的文本位于调整文本大小的代码下方,但文本框和图片框的位置移动不同,这意味着当我拉动整个页面时,文本框一个向上移动,另外两个向下移动.. ..见下图链接。
public Font FlexFont(Graphics g, float minFontSize, float maxFontSize, Size layoutSize, string s, Font f, out SizeF extent)
{
if (maxFontSize == minFontSize)
f = new Font(f.FontFamily, minFontSize, f.Style);
extent = g.MeasureString(s, f);
if (maxFontSize <= minFontSize)
return f;
float hRatio = layoutSize.Height / extent.Height;
float wRatio = layoutSize.Width / extent.Width;
float ratio = (hRatio < wRatio) ? hRatio : wRatio;
float newSize = f.Size * ratio;
if (newSize < minFontSize)
newSize = minFontSize;
else if (newSize > maxFontSize)
newSize = maxFontSize;
f = new Font(f.FontFamily, newSize, f.Style);
extent = g.MeasureString(s, f);
return f;
}
internal static void OnPaint(object sender, EventArgs e, string text)
{
throw new NotImplementedException();
}
public void OnPaint(object sender, PaintEventArgs e, string text)
{
var control = sender as Control;
if (control == null)
return;
control.Text = string.Empty; //delete old stuff
var rectangle = control.ClientRectangle;
using (Font f = new Font("Microsoft Sans Serif", 20.25f, FontStyle.Bold))
{
SizeF size;
using (Font f2 = FlexFont(e.Graphics, 5, 50, rectangle.Size, text, f, out size))
{
PointF p = new PointF((rectangle.Width - size.Width) / 3, (rectangle.Height - size.Height) / 3);
e.Graphics.DrawString(text, f2, Brushes.Black, p);
}
}
页面截图:
原始页面不拉大页面
- https://ibb.co/P6s8hbZ
拉大页面后
- https://ibb.co/jvn7nfk
【问题讨论】:
标签: c# tablelayout