[英]How to auto resize and adjust Form controls with change in resolution
我注意到一些应用程序会改变它们的控件位置以尽可能地调整它们的分辨率,如果窗口最大化,它们会以这样的方式设置自己,使整个 GUI 看起来平衡。 我的问题是是否可以在 Visual Studio 2010 C# 中创建或实现此功能?
使用这些组合来获得所需的结果:
将Anchor
属性设置为 None,控件不会调整大小,它们只会移动它们的位置。
将Anchor
属性设置为 Top+Bottom+Left+Right,控件将调整大小,但不会改变它们的位置。
将表单的Minimum Size
设置为适当的值。
设置Dock
属性。
使用Form Resize
事件更改您想要的任何内容
我不知道字体大小(标签、文本框、组合框等)在 (1) - (4) 中会受到什么影响,但可以在 (5) 中进行控制。
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);
}
..并检测分辨率的变化来处理它(一旦你像 SwDevMan81 建议的那样使用停靠和锚定)使用Microsoft.Win32 中的SystemEvents.DisplaySettingsChanged 事件。
这里我喜欢使用https://www.netresize.net/index.php?c=3a&id=11#buyopt 。 不过是付费版。
如果您购买 1 个站点许可证(无限开发人员),您也可以获得他们的源代码。
我是如何找到 nuget 包解决方案的。
抱歉,我看到问题晚了,这是一个简单的编程解决方案,对我很有效,
创建这些全局变量:
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);
}
}
我希望这会有所帮助,它对我的项目非常有效。
在页面加载时为所有控件添加此代码或在容器中添加所有控件
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;
在表单加载事件中添加这一行
this.WindowState = FormWindowState.Maximized;
private void MainForm_Load( object sender, EventArgs e )
{
this.Size = Screen.PrimaryScreen.WorkingArea.Size
}
this.WindowState = FormWindowState.Maximized;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.