2014-01-22 265 views
1

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

如何为我的表单设置绝对1280X800,并确保表格大小不会改变!

+0

您可以设置最小尺寸,但如果没有足够的屏幕空间,那么该窗口将延伸到可视显示屏之外。我建议你使用'Anchor'属性来控制控件,并在需要的时候使用滚动面板......等到你的一个用户决定要改变DPI设置时,这将会更有趣! – musefan

回答

2

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

希望这有助于。

+0

这就是为什么我喜欢和错过WPF/WinForms :(它太sooooo令人惊讶...挤满了功能和C#... OH C#!哦...只是...爱C#! – Subby

2

使用窗体的MaximumSize属性。

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

您还可以设置一个如果的minimumSize你不希望用户能够使它比期望的大小更小。

1

酒店

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

希望这会有所帮助。