我注意到一些应用程序会更改它们的控件位置以尽可能多地调整它们的分辨率,如果窗口最大化,它们会以这样一种方式设置自己,以使所有 GUI 看起来都是平衡的。我的问题是是否可以在 Visual Studio 2010 C# 中制作或实现此功能?
10 回答
使用这些组合来获得所需的结果:
将
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);
}
..并检测分辨率的变化来处理它(一旦你像 SwDevMan81 建议的那样使用对接和锚定)使用Microsoft.Win32中的SystemEvents.DisplaySettingsChanged 事件。
抱歉,我看到这个问题晚了,这是一个简单的编程解决方案,对我很有效,
创建这些全局变量:
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);
}
}
我希望这会有所帮助,它适用于我的项目。
在这里,我喜欢使用https://www.netresize.net/index.php?c=3a&id=11#buyopt。但它是付费版本。
如果您购买 1 Site License (Unlimited Developers),您也可以获得他们的源代码。
我如何找到 nuget 包解决方案。
在表单加载事件中添加这一行
this.WindowState = FormWindowState.Maximized;
在页面加载时为所有控件添加此代码或在容器中添加所有控件
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;
private void MainForm_Load( object sender, EventArgs e )
{
this.Size = Screen.PrimaryScreen.WorkingArea.Size
}
this.WindowState = FormWindowState.Maximized;