C# WinForm屏幕分辨率

3

我有一个C# WinForms应用程序,当我把可执行文件交给不同的用户时,该应用程序会根据他们的屏幕分辨率显示不同大小。一些应用程序的部分可能无法显示。

我该如何为我的窗体设置绝对大小1280X800,并确保窗体大小无论分辨率如何都不会改变!


你可以设置最小尺寸,但如果屏幕空间不足,则窗口将超出可见显示范围。我建议您在控件上使用“Anchor”属性,并在需要时使用滚动面板...只要有一个用户决定更改DPI设置,那将会更有趣! - musefan
4个回答

4

您可以使用 控件.ScaleControl控件.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);
   }
}

希望这能帮到您。

1
这就是为什么我喜欢和想念WPF/WinForms :( 它们如此地...充满了功能和C#...OH C#! 哦...只是...爱那个C#! - Subby

3

使用窗体的MaximumSize属性。

form.MaximumSize = new Size(1280, 800);

如果您不希望用户将其缩小到所需大小以下,还可以设置MinimumSize。


2
属性
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变量用于计算屏幕大小时排除任务栏和其他停靠在桌面上的项目。

希望这有所帮助。


2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接