如何通过更改分辨率自动调整大小和调整窗体控件

时间:2010-11-22 18:05:30

标签: c# winforms user-interface resize screen-resolution

我注意到一些应用程序会更改其控件位置,以尽可能在分辨率中调整它们。如果窗口最大化,它们将自己设置为使所有GUI看起来均衡。 我的问题是,是否可以在Visual Studio 2010 C#中制作或实现此功能?

10 个答案:

答案 0 :(得分:61)

使用DockAnchor属性。 Here是一篇很好的文章。请注意,这些将在最大化/最小化时处理更改。这有点不同,如果屏幕分辨率发生变化,但它会有相同的想法。

答案 1 :(得分:18)

使用这些组合来获得所需的结果:

  1. Anchor属性设置为无,控件不会调整大小,只会改变位置。

  2. Anchor属性设置为顶部+底部+左侧+右侧,控件将调整大小,但不会更改其位置。

  3. 将表单的Minimum Size设置为正确的值。

  4. 设置Dock属性。

  5. 使用Form Resize事件更改您想要的内容

  6. 我不知道(1) - (4)中字体大小(标签,文本框,组合框等)会受到影响,但可以在(5)中控制。

答案 2 :(得分:9)

float widthRatio = Screen.PrimaryScreen.Bounds.Width / 1280;
float heightRatio = Screen.PrimaryScreen.Bounds.Height / 800f;
SizeF scale = new SizeF(widthRatio, heightRatio);
this.Scale(scale);
foreach (Control control in this.Controls)
{
control.Font = new Font("Verdana", control.Font.SizeInPoints * heightRatio * widthRatio);
}

答案 3 :(得分:2)

..并检测处理它的分辨率变化(一旦你使用像建议的SwDevMan81那样使用Docking和Anchoring),请使用SystemEvents.DisplaySettingsChanged event中的Microsoft.Win32

答案 4 :(得分:0)

在表单加载事件中添加此行

this.WindowState = FormWindowState.Maximized;

答案 5 :(得分:0)

在这里我喜欢使用https://www.netresize.net/index.php?c=3a&id=11#buyopt。但这是付费版本。

如果您购买1个站点许可证(无限的开发人员),您还可以获得其源代码。

我如何找到nuget软件包解决方案。

答案 6 :(得分:0)

抱歉,我迟到了这个问题, 这是一个简单的编程解决方案,对我很有效,

创建这些全局变量:

 float firstWidth;
 float firstHeight;

加载后,填充这些变量;

 firstWidth = this.Size.Width;
 firstHeight = this.Size.Height;

然后选择您的表单并将这些代码放入表单的SizeChange事件中;

 private void AnaMenu_SizeChanged(object sender, EventArgs e)
    {
        

        float size1 = this.Size.Width /  firstWidth;
        float size2 = this.Size.Height / firstHeight;

            SizeF scale = new SizeF(size1, size2);
        firstWidth = this.Size.Width;
        firstHeight = this.Size.Height;

        foreach (Control control in this.Controls)
        {
                
            control.Font = new Font(control.Font.FontFamily, control.Font.Size* ((size1+ size2)/2));
            
            control.Scale(scale);
                

        }


    }

我希望这会有所帮助,在我的项目中能完美运行。

答案 7 :(得分:-1)

在页面加载时为所有控件添加此代码或在容器中添加所有控件

int x;
Point pt = new Point();
x = Screen.PrimaryScreen.WorkingArea.Width - 1024;
x = x / 2;
pt.Y = groupBox1.Location.Y + 50;
pt.X = groupBox1.Location.X + x;
groupBox1.Location = pt;

答案 8 :(得分:-1)

private void MainForm_Load( object sender, EventArgs e ) 
     { 
        this.Size = Screen.PrimaryScreen.WorkingArea.Size 
     }

答案 9 :(得分:-2)

this.WindowState = FormWindowState.Maximized;
相关问题