2014-01-22 298 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用於在計算屏幕大小時排除桌面上的任務欄和其他停靠項目等內容。

希望這會有所幫助。