使表单适合各种尺寸的屏幕

Make forms fit to every size of screen

我用 c# 开发了我的第一个 windows 表单应用程序,但是当 运行 它在屏幕尺寸不同的另一台计算机上时,控件不在它们应该在的位置。

我用过

this.WindowState = FormWindowState.Maximized;

最大化屏幕,但这样做时窗体只是在底部扩展,控件保持在相同位置。

使用 anchordock 属性 进行所需的控制。

Dock : Dock 属性 强制控件像胶水一样粘在父窗体(或控件)的特定边缘。

例如:

如果您有一个图像框并将停靠栏设置在右下角,那么在您最大化 windows 后,图像框将保留在右下角。

您还可以将 "Fill" 用于 Dock 属性 以根据 windows 大小动态调整大小。

HI 对于响应式设计 第一次在 class

以下创建
 public class Resolution
    {
        float heightRatio = new float();
        float widthRatio = new float();
        int standardHeight, standardWidth;
        public void ResizeForm(Form objForm, int DesignerHeight, int DesignerWidth)
        {
            standardHeight = DesignerHeight;
            standardWidth = DesignerWidth;
            int presentHeight = Screen.PrimaryScreen.WorkingArea.Height;//.Bounds.Height;
            int presentWidth = Screen.PrimaryScreen.Bounds.Width;
            heightRatio = (float)((float)presentHeight / (float)standardHeight);
            widthRatio = (float)((float)presentWidth / (float)standardWidth);
            objForm.AutoScaleMode = AutoScaleMode.None;
            objForm.Scale(new SizeF(widthRatio, heightRatio));
            foreach (Control c in objForm.Controls)
            {
                if (c.HasChildren)
                {
                    ResizeControlStore(c);
                }
                else
                {
                    c.Font = new Font(c.Font.FontFamily, c.Font.Size * heightRatio, c.Font.Style, c.Font.Unit, ((byte)(0)));
                }
            }
            objForm.Font = new Font(objForm.Font.FontFamily, objForm.Font.Size * heightRatio, objForm.Font.Style, objForm.Font.Unit, ((byte)(0)));
        }

        private void ResizeControlStore(Control objCtl)
        {
            if (objCtl.HasChildren)
            {
                foreach (Control cChildren in objCtl.Controls)
                {
                    if (cChildren.HasChildren)
                    {
                        ResizeControlStore(cChildren);

                    }
                    else
                    {
                        cChildren.Font = new Font(cChildren.Font.FontFamily, cChildren.Font.Size * heightRatio, cChildren.Font.Style, cChildren.Font.Unit, ((byte)(0)));
                    }
                }
                objCtl.Font = new Font(objCtl.Font.FontFamily, objCtl.Font.Size * heightRatio, objCtl.Font.Style, objCtl.Font.Unit, ((byte)(0)));
            }
            else
            {
                objCtl.Font = new Font(objCtl.Font.FontFamily, objCtl.Font.Size * heightRatio, objCtl.Font.Style, objCtl.Font.Unit, ((byte)(0)));
            }
        }
    }

then when ever yo add any form the add panel control to form and dock it to form below
InitializeComponent();

写下代码

    this.WindowState = FormWindowState.Maximized;
    int screenWidth = Screen.PrimaryScreen.Bounds.Width;
    int screenHeight = Screen.PrimaryScreen.Bounds.Height;
    Resolution objFormResizer = new Resolution();
    objFormResizer.ResizeForm(this, screenHeight, screenWidth); 

这将使表单尽可能响应并创建系统默认字体