【问题标题】:Controls resize based on screen resolution控制根据屏幕分辨率调整大小
【发布时间】:2011-02-17 14:58:04
【问题描述】:

我有面板控制。面板中有更多控件。我将面板的停靠属性设置为“填充”。面板根据屏幕分辨率调整大小。但控件保持不变。面板中的控件不会根据屏幕解决方案调整大小。

我在同一页面中有更多标签、面板、文本框和按钮。

如何设置停靠属性以根据屏幕分辨率调整页面中所有控件的大小?

感谢您的帮助

【问题讨论】:

  • Dock 和 Anchor 属性仅适用于“布局”。 AutoScaleMode 和 AutoScaleDimensions 属性用于“屏幕分辨率”更改。

标签: winforms controls resize resolution screens


【解决方案1】:

使用 Anchor 属性并将控件锚定到所有 4 个边。

【讨论】:

    【解决方案2】:

    我希望这个解决方案(来自here)将有助于在客户端屏幕分辨率发生变化时显示表单内的所有控件:

    int i_StandardHeight = 768;//Developer Desktop Height Where the Form is Designed
                    int i_StandardWidth = 1024; ;//Developer Desktop Width Where the Form is Designed
                    int i_PresentHeight = Screen.PrimaryScreen.Bounds.Height;
                    int i_PresentWidth = Screen.PrimaryScreen.Bounds.Width;
                    float f_HeightRatio = new float();
                    float f_WidthRatio = new float();
                    f_HeightRatio = (float)((float)i_PresentHeight / (float)i_StandardHeight);
                    f_WidthRatio = (float)((float)i_PresentWidth / (float)i_StandardWidth);
                    foreach (Control c in this.Controls)
                    {
                        if (c.GetType().ToString() == "System.Windows.Forms.Button")
                        {
                            Button obtn = (Button)c;
                            obtn.TextAlign = ContentAlignment.MiddleCenter;
                        }
                        if (c.HasChildren)
                        {
                            foreach (Control cChildren in c.Controls)
                            {
                                cChildren.SetBounds(Convert.ToInt32(cChildren.Bounds.X * f_WidthRatio), Convert.ToInt32(cChildren.Bounds.Y * f_WidthRatio), Convert.ToInt32(cChildren.Bounds.Width * f_WidthRatio), Convert.ToInt32(cChildren.Bounds.Height * f_HeightRatio));
                                //cChildren.Font = new Font(cChildren.Font.FontFamily, cChildren.Font.Size * f_HeightRatio, cChildren.Font.Style, cChildren.Font.Unit, ((byte)(0)));
                            }
                            c.SetBounds(Convert.ToInt32(c.Bounds.X * f_WidthRatio), Convert.ToInt32(c.Bounds.Y * f_WidthRatio), Convert.ToInt32(c.Bounds.Width * f_WidthRatio), Convert.ToInt32(c.Bounds.Height * f_HeightRatio));
                           // c.Font = new Font(c.Font.FontFamily, c.Font.Size * f_HeightRatio, c.Font.Style, c.Font.Unit, ((byte)(0)));
                        }
                        else
                        {
                            c.SetBounds(Convert.ToInt32(c.Bounds.X * f_WidthRatio), Convert.ToInt32(c.Bounds.Y * f_WidthRatio), Convert.ToInt32(c.Bounds.Width * f_WidthRatio), Convert.ToInt32(c.Bounds.Height * f_HeightRatio));
                           // c.Font = new Font(c.Font.FontFamily, c.Font.Size * f_HeightRatio, c.Font.Style, c.Font.Unit, ((byte)(0)));
                        }
                    }
                    this.Height = Convert.ToInt32(i_StandardHeight * f_HeightRatio);
                    this.Width = Convert.ToInt32(i_StandardWidth * f_WidthRatio); 
    

    【讨论】:

      【解决方案3】:

      除了设置容器 Panel 的 Dock 属性外,还必须设置 Panel 内控件的 Anchor 或 Dock 属性。通常,当您在表单上有多个控件时,添加 TableLayoutPanel、FlowLayoutPanel 甚至另一个 Panel 会有所帮助。

      【讨论】:

        猜你喜欢
        • 2014-09-21
        • 2014-06-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多