![](/img/trans.png)
[英]How to fit windows form to any screen resolution and how to made my image size fit to form
[英]How to fit Windows Form to any screen resolution?
我使用 C# 在 VS 2008 上工作。 下面的代碼對我不起作用。 我的表單設計為 1024 x 768 分辨率。
我們的客戶筆記本電腦的分辨率為 1366 x 768。 為了解決這個問題,我在表單加載事件中設置了以下代碼:
this.Location = new Point(0, 0);
this.Size = Screen.PrimaryScreen.WorkingArea.Size;
但是表單不會根據屏幕分辨率調整大小,並且我的表單底部被隱藏或剪切,或者我錯過了滾動條。
有沒有辦法解決這個問題? 請告訴我語法。 提前致謝
你不能開始最大化嗎?
將System.Windows.Forms.Form.WindowState
屬性設置為FormWindowState.Maximized
如果要以編程方式設置表單大小,請將表單的StartPosition
屬性設置為Manual
。 否則表單自身的定位和大小調整算法會干擾您的。 這就是您遇到問題中提到的問題的原因。
示例:這是我如何將表單大小調整為原始大小和屏幕工作區域大小之間的一半大小。 我還將表格置於工作區域的中心:
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);
}
請注意,單獨將WindowState
設置為FormWindowState.Maximized
不會更改已還原窗口的大小。 因此,只要窗口最大化,它可能看起來不錯,但是當恢復時,窗口大小和位置仍然可能是錯誤的。 因此,即使您打算將窗口打開為最大化,我也建議設置大小和位置。
可能最大化表單會有所幫助,或者您可以在表單加載時手動執行此操作:
代碼塊
this.Location = new Point(0, 0);
this.Size = Screen.PrimaryScreen.WorkingArea.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;
只需為您的 Windows 窗體設置Autoscroll = true
..(它不是很好的解決方案,但很有幫助)..
也嘗試面板( Autoscroll
屬性 = true
)
你總是可以告訴窗口以最大化開始......它應該給你同樣的結果......就像這樣: this.WindowState = FormWindowState.Maximized;
PS您也可以嘗試(我不建議這樣做)減去任務欄高度。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.