繁体   English   中英

通过动态更改字体大小来调整按钮控件中的文本

[英]Fit the text in the button control on resize by changing Fontsize dynamically

我有一个应用程序,其中每 5 秒在按钮上设置不同长度的文本。 如何调整文本大小以适合按钮。 我还需要在调整 window 大小时调整字体大小。(我的按钮大小在调整 window 大小时会增加,因为我在其上使用了停靠属性。

以下是我用来执行此操作的代码,但当文本长度为 2 或更少时,它不能很好地工作。(文本弹出有点超出控制)

public static void FitControlFont(Control control)
        {
            if (control.Text.Length == 0)
            {
                return;
            }

            try
            {
                Font currentFont = control.Font;
                Graphics graphics = control.CreateGraphics();
                SizeF newSize = graphics.MeasureString(control.Text, control.Font);
                graphics.Dispose();

                float factorX = control.Width / newSize.Width;
                float factorY = control.Height / newSize.Height;
                float factor = factorX > factorY ? factorY : factorX;
                if (control.InvokeRequired)
                {
                    control.Invoke(new MethodInvoker(delegate { control.Font = new Font(currentFont.Name, currentFont.SizeInPoints * factor); }));
                }
                else
                {
                    control.Font = new Font(currentFont.Name, currentFont.SizeInPoints * factor);
                }
            }
            catch (Exception ex)
            {
                if (Exceptions.IsCritical(ex))
                {
                    throw;
                }

                return;
            }
        }

您可以将按钮的autosize属性设置为true,这将使其宽度适合内容。

关于更改字体,您可以处理按钮的SizeChanged(因为您将其设置为dock属性),并确定字体相对于表单高度的比例:

private void button1_SizeChanged(object sender, EventArgs e)
{
     button1.Font = new Font(button1.Font.FontFamily, this.Size.Height / 10) ;
}

I improved the function a bit because in your example the control.Width and newSize.Width are integer, control.Height and newSize.Height are also integer, so when you calculate the form factors you get an integer result and cast it as float,这不是一个好的外形(并且可以使新创建的Font崩溃为零):

        public static void FitControlFont(Control control)
        {
            if (control.Text.Length == 0)
            {
                return;
            }

            try
            {
                Font currentFont = control.Font;
                Graphics graphics = control.CreateGraphics();
                SizeF newSize = graphics.MeasureString(control.Text, control.Font);
                graphics.Dispose();

                float factorX = ((float)control.Width) / newSize.Width;
                float factorY = ((float)control.Height) / newSize.Height;
                float factor = factorX > factorY ? factorY : factorX;
                if (control.InvokeRequired)
                {
                    control.Invoke(new MethodInvoker(delegate { control.Font = new Font(currentFont.Name, currentFont.SizeInPoints * factor); }));
                }
                else
                {
                    control.Font = new Font(currentFont.Name, currentFont.SizeInPoints * factor);
                }
            }
            catch (Exception ex)
            {
                if (Exceptions.IsCritical(ex))
                {
                    throw;
                }

                return;
            }
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM