我有一个C#WinForms应用程序,当我将可执行文件提供给不同的用户时,应用程序以不同的大小显示(基于它们的屏幕分辨率)。 应用程序的某些部分无法看到。
我怎样才能为我的表单设置绝对的1280X800,并确保不pipe分辨率如何,表格大小都不会改变!
您可以使用Control.ScaleControl和Control.Scale
private void MainForm_Load( object sender, EventArgs e ) { float width_ratio = (Screen.PrimaryScreen.Bounds.Width / 1280); float heigh_ratio = (Screen.PrimaryScreen.Bounds.Height / 800f); SizeF scale = new SizeF(width_ratio, heigh_ratio); this.Scale(scale); //And for font size foreach (Control control in this.Controls) { control.Font = new Font("Microsoft Sans Serif", c.Font.SizeInPoints * heigh_ratio * width_ratio); } }
希望这可以帮助。
使用窗体的MaximumSize属性。
form.MaximumSize = new Size(1280, 800);
如果您不希望用户使其小于所需大小,也可以设置MinimumSize。
您可以改为设计GUI,以便更容易地上下滚动。您可以使用以下内容
布局经理
对接
锚
财产
Screen.PrimaryScreen.WorkingArea
对于表单大小和定位非常有用。 例如这个代码:
this.Width = Screen.PrimaryScreen.WorkingArea.Width/2; this.Height = Screen.PrimaryScreen.WorkingArea.Height/2; this.Top = (Screen.PrimaryScreen.WorkingArea.Top + Screen.PrimaryScreen.WorkingArea.Height)/4; this.Left = (Screen.PrimaryScreen.WorkingArea.Left + Screen.PrimaryScreen.WorkingArea.Width)/4;
会将其执行的表单放在屏幕中间,并将其大小设置为屏幕的一半。
在计算屏幕大小时,WorkingArea var用于排除桌面上的任务栏和其他停靠项目等内容。
希望这可以帮助。