【发布时间】:2011-05-14 00:20:57
【问题描述】:
我注意到一些应用程序会更改它们的控件位置以尽可能地调整它们的分辨率,如果窗口最大化,它们会以这样一种方式设置自己,以使所有 GUI 看起来平衡。 我的问题是是否可以在 Visual Studio 2010 C# 中制作或实现此功能?
【问题讨论】:
标签: c# winforms user-interface resize screen-resolution
我注意到一些应用程序会更改它们的控件位置以尽可能地调整它们的分辨率,如果窗口最大化,它们会以这样一种方式设置自己,以使所有 GUI 看起来平衡。 我的问题是是否可以在 Visual Studio 2010 C# 中制作或实现此功能?
【问题讨论】:
标签: c# winforms user-interface resize screen-resolution
使用这些组合来获得所需的结果:
将Anchor 属性设置为无,控件不会调整大小,它们只会移动位置。
将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);
}
【讨论】:
AutoScaleMode,其形式为Font。
..并检测分辨率的变化来处理它(一旦你像 SwDevMan81 建议的那样使用对接和锚定)使用SystemEvents.DisplaySettingsChanged event in Microsoft.Win32。
【讨论】:
这里我喜欢使用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);
}
}
我希望这会有所帮助,它适用于我的项目。
【讨论】:
control.Font = new Font(control.Font.FontFamily, control.Font.Size* ((size1+ size2)/2)); 行中出现错误 Value of '∞' is not valid for 'emSize'. 'emSize' should be greater than 0 and less than or equal to System.Single.MaxValue. Parameter name: emSize。
在页面加载时为所有控件添加此代码或在容器中添加所有控件
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;
【讨论】: