简体   繁体   中英

Resize the textBox and pictureBox in C# windowform

The text in the label is below the code to resize the text size but the textbox and PictureBox position are moving different, that's mean when I pull the whole page the textbox is one is moving up and another two is moving down....see the below picture link.

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);
            }
        }

Page Screenshots :

original page without pull big the page
- https://ibb.co/P6s8hbZ

after pulling big the page
- https://ibb.co/jvn7nfk

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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