【问题标题】:c# winform screen resolutionc# winform 屏幕分辨率
【发布时间】:2014-01-22 11:16:13
【问题描述】:

我有一个 C# WinForms 应用程序,当我将可执行文件提供给不同的用户时,应用程序会以不同的大小显示(基于他们的屏幕分辨率)。无法看到应用程序的某些部分。

如何为我的表单设置绝对 1280X800 并确保表单大小不会改变,无论分辨率是多少!

【问题讨论】:

  • 您可以设置最小尺寸,但如果没有足够的屏幕空间,则窗口将超出可见显示。我建议您使用 Anchor 属性进行控件,并在需要时使用滚动面板...只需等到您的一位用户决定他们想要更改 DPI 设置,那会更有趣!

标签: c# windows winforms


【解决方案1】:

您可以使用Control.ScaleControlControl.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);
   }
}

希望这会有所帮助。

【讨论】:

  • 这就是我喜欢和怀念 WPF/WinForms 的原因:(它太神奇了...包含许多功能和 C#...哦 C#!哦...只是...喜欢那个 C#!
【解决方案2】:

使用表单的MaximumSize 属性。

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

如果您不希望用户将其设置为小于所需大小,您也可以设置一个 MinimumSize。

【讨论】:

    【解决方案3】:

    您可以改为设计 GUI,使其更容易上下滚动。您可以使用以下内容

    Layout Managers

    Docking

    Anchors

    【讨论】:

      【解决方案4】:

      房产

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

      希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-07-04
        • 1970-01-01
        • 2016-06-28
        • 1970-01-01
        相关资源
        最近更新 更多