简体   繁体   English

如何使 Windows 窗体适合任何屏幕分辨率?

[英]How to fit Windows Form to any screen resolution?

I work on VS 2008 with C#.我使用 C# 在 VS 2008 上工作。 This below code does not work for me.下面的代码对我不起作用。 My form was designed in 1024 x 768 resolution.我的表单设计为 1024 x 768 分辨率。

Our clients laptop is in 1366 x 768 resolution.我们的客户笔记本电脑的分辨率为 1366 x 768。 To solve this problem, I set below code in Form Load event:为了解决这个问题,我在表单加载事件中设置了以下代码:

this.Location = new Point(0, 0);
this.Size = Screen.PrimaryScreen.WorkingArea.Size;

but the form does not resize as per screen resolution and bottom of my form gets hidden or cut or I miss the scroll bar.但是表单不会根据屏幕分辨率调整大小,并且我的表单底部被隐藏或剪切,或者我错过了滚动条。

Is there any way to solve this problem?有没有办法解决这个问题? Please show me the syntax.请告诉我语法。 Thanks in advance提前致谢

Can't you start maximized?你不能开始最大化吗?

Set the System.Windows.Forms.Form.WindowState property to FormWindowState.MaximizedSystem.Windows.Forms.Form.WindowState属性设置为FormWindowState.Maximized

If you want to set the form size programmatically, set the form's StartPosition property to Manual .如果要以编程方式设置表单大小,请将表单的StartPosition属性设置为Manual Otherwise the form's own positioning and sizing algorithm will interfere with yours.否则表单自身的定位和大小调整算法会干扰您的。 This is why you are experiencing the problems mentioned in your question.这就是您遇到问题中提到的问题的原因。

Example: Here is how I resize the form to a size half-way between its original size and the size of the screen's working area.示例:这是我如何将表单大小调整为原始大小和屏幕工作区域大小之间的一半大小。 I also center the form in the working area:我还将表格置于工作区域的中心:

public MainView()
{
    InitializeComponent();

    // StartPosition was set to FormStartPosition.Manual in the properties window.
    Rectangle screen = Screen.PrimaryScreen.WorkingArea;
    int w = Width >= screen.Width ? screen.Width : (screen.Width + Width) / 2;
    int h = Height >= screen.Height ? screen.Height : (screen.Height + Height) / 2;
    this.Location = new Point((screen.Width - w) / 2, (screen.Height - h) / 2);
    this.Size = new Size(w, h);
}

Note that setting WindowState to FormWindowState.Maximized alone does not change the size of the restored window.请注意,单独将WindowState设置为FormWindowState.Maximized不会更改已还原窗口的大小。 So the window might look good as long as it is maximized, but when restored, the window size and location can still be wrong.因此,只要窗口最大化,它可能看起来不错,但是当恢复时,窗口大小和位置仍然可能是错误的。 So I suggest setting size and location even when you intend to open the window as maximized.因此,即使您打算将窗口打开为最大化,我也建议设置大小和位置。

Probably a maximized Form helps, or you can do this manually upon form load:可能最大化表单会有所帮助,或者您可以在表单加载时手动执行此操作:

Code Block代码块

this.Location = new Point(0, 0);

this.Size = Screen.PrimaryScreen.WorkingArea.Size;

And then, play with anchoring, so the child controls inside your form automatically fit in your form's new size.然后,使用锚定,以便表单内的子控件自动适应表单的新大小。

将表单属性设置为以最大化状态打开。

this.WindowState = FormWindowState.Maximized;
int h = Screen.PrimaryScreen.WorkingArea.Height;
int w = Screen.PrimaryScreen.WorkingArea.Width;
this.ClientSize = new Size(w , h);

您可以简单地设置窗口状态

this.WindowState = System.Windows.Forms.FormWindowState.Maximized;

simply set Autoscroll = true for ur windows form.. (its not good solution but helpful)..只需为您的 Windows 窗体设置Autoscroll = true ..(它不是很好的解决方案,但很有帮助)..

try for panel also( Autoscroll property = true )也尝试面板( Autoscroll属性 = true

You can always tell the window to start in maximized... it should give you the same result... Like this: this.WindowState = FormWindowState.Maximized;你总是可以告诉窗口以最大化开始......它应该给你同样的结果......就像这样: this.WindowState = FormWindowState.Maximized;

PS You could also try (and I'm not recommending this) to subtract the taskbar height. PS您也可以尝试(我不建议这样做)减去任务栏高度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM