【问题标题】:Make forms fit to every size of screen使表格适合各种尺寸的屏幕
【发布时间】:2018-10-14 12:10:20
【问题描述】:

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

我用过

this.WindowState = FormWindowState.Maximized;

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

【问题讨论】:

  • 是的,控件永远不会移动,除非它们停靠在某个地方,在你的情况下它们可能不是。解决方法是将autoscalemode 设置为None,将StartPosition 设置为CentreScreen,将MinimizeBox 设置为false。如果由于某种原因无法执行上述操作,则需要相互使用停靠和锚点控件。
  • 看看这个question/answer 它提供了一个很好的讨论自动缩放和winforms
  • 在 WinForms 中使用面板和停靠属性是你的朋友。

标签: c# winforms user-interface user-controls


【解决方案1】:

为您需要的控件使用 anchordock 属性。

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

例如:

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

您还可以对您的 Dock 属性使用“填充”,以使大小也根据窗口大小动态变化。

【讨论】:

    【解决方案2】:

    HI 响应式设计 第一次创建下面的类

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

    然后,当您添加任何表单时,将面板控件添加到表单和停靠 它形成 下面
    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); 
    

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

    【讨论】:

    • 谢谢,我结合了答案,现在它可以工作了:) 但是当我使用这段代码时,我的表单中 datagridview 的字体单元格样式发生了变化。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-24
    • 1970-01-01
    • 2016-07-02
    • 2016-07-02
    • 1970-01-01
    • 2015-12-06
    • 1970-01-01
    相关资源
    最近更新 更多