如何随着分辨率的变化自动调整大小和调整表单控件

发布于 2024-10-04 00:11:58 字数 121 浏览 5 评论 0原文

我注意到某些应用程序会更改控件的位置以尽可能适应当前的分辨率。例如,如果窗口最大化,则控件的设置方式应使整个 GUI 看起来平衡。

是否可以使用 C# 在 Visual studio 2010 中制作或实现此功能?

I have noticed that some applications change their controls' positions to fit themselves as much as possible in the current resolution. For example, if the window is maximized, the controls are set in such a way that the overall GUI looks balanced.

Is it possible to make or implement this functionality in Visual studio 2010 using C#?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

离线来电— 2024-10-11 00:11:58

使用 Dock锚点属性。 这里是一个很好的文章。请注意,这些将在最大化/最小化时处理更改。如果屏幕分辨率发生变化,情况会略有不同,但原理相同。

Use Dock and Anchor properties. Here is a good article. Note that these will handle changes when maximizing/minimizing. That is a little different that if the screen resolution changes, but it will be along the same idea.

吃兔兔 2024-10-11 00:11:58

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

  1. Anchor属性设置为None,控件将不会调整大小,它们只会移动其位置。

  2. Anchor 属性设置为 Top+Bottom+Left+Right,控件将调整大小,但不会改变其位置。

  3. 将表单的最小大小设置为适当的值。

  4. 设置Dock 属性。

  5. 使用Form Resize事件来更改你想要的任何内容

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

Use combinations of these to get the desired result:

  1. Set Anchor property to None, the controls will not be resized, they only shift their position.

  2. Set Anchor property to Top+Bottom+Left+Right, the controls will be resized but they don't change their position.

  3. Set the Minimum Size of the form to a proper value.

  4. Set Dock property.

  5. Use Form Resize event to change whatever you want

I don't know how font size (label, textbox, combobox, etc.) will be affected in (1) - (4), but it can be controlled in (5).

你曾走过我的故事 2024-10-11 00:11:58
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);
}
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);
}
不寐倦长更 2024-10-11 00:11:58

..并检测分辨率的变化来处理它(一旦您像 SwDevMan81 建议的那样使用对接和锚定),请使用 SystemEvents.DisplaySettingsChanged 事件 in Microsoft.Win32

..and to detect a change in resolution to handle it (once you're using Docking and Anchoring like SwDevMan81 suggested) use the SystemEvents.DisplaySettingsChanged event in Microsoft.Win32.

凯凯我们等你回来 2024-10-11 00:11:58

抱歉我迟到了才看到问题
这是一个简单的编程解决方案,对我来说效果很好,

创建这些全局变量:

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

        }


    }

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

sorry I saw the question late,
Here is an easy programmatically solution that works well on me,

Create those global variables:

 float firstWidth;
 float firstHeight;

after on load, fill those variables;

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

then select your form and put these code to your form's SizeChange event;

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

        }


    }

I hope this helps, it works perfect on my projects.

怀里藏娇 2024-10-11 00:11:58

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

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

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

Here I like to use https://www.netresize.net/index.php?c=3a&id=11#buyopt. But it is paid version.

You also can get their source codes if you buy 1 Site License (Unlimited Developers).

How ever I am finding the nuget package solution.

梦晓ヶ微光ヅ倾城 2024-10-11 00:11:58

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

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;

add this code at page load do for all control or add all control in containers

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;
一笔一画续写前缘 2024-10-11 00:11:58

在表单加载事件中添加这一行

this.WindowState = FormWindowState.Maximized;

in the form load event add this line

this.WindowState = FormWindowState.Maximized;
又爬满兰若 2024-10-11 00:11:58
private void MainForm_Load( object sender, EventArgs e ) 
     { 
        this.Size = Screen.PrimaryScreen.WorkingArea.Size 
     }
private void MainForm_Load( object sender, EventArgs e ) 
     { 
        this.Size = Screen.PrimaryScreen.WorkingArea.Size 
     }
剑心龙吟 2024-10-11 00:11:58
this.WindowState = FormWindowState.Maximized;
this.WindowState = FormWindowState.Maximized;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文