我注意到有些应用程序会更改其控件的位置,以使自己尽可能地适应当前的分辨率。例如,如果窗口最大化,控件被设置在一种方式,使得整体GUI看起来平衡。
在Visual Studio 2010中使用C#是否可以实现这个功能?
我注意到有些应用程序会更改其控件的位置,以使自己尽可能地适应当前的分辨率。例如,如果窗口最大化,控件被设置在一种方式,使得整体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);
}
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个站点许可证(无限开发人员),您也可以获得他们的源代码。
然而,我正在寻找NuGet包解决方案。
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;