在C#窗体中调整文本框和图片框的大小

问题描述 投票:0回答:1

标签中的文字位于代码下方以调整文本大小,但文本框和PictureBox位置的移动方式不同,这意味着当我拉动整个页面时,文本框正在向上移动,另外两个向下移动....下面的图片链接。

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
1个回答
0
投票

在“生成的.cs”文件中检查“名称”文本框和“合同编号”文本框以及字体属性的对齐属性和对接属性,与“地址”文本框设置代码进行比较

© www.soinside.com 2019 - 2024. All rights reserved.