其實winform並不適合做自適應這玩意的,雖然,能做是能做,
就像菜刀一樣能切水果,但是,不是那么合適,畢竟有水果刀
wpf做自適應拉伸窗體,會更好一些。
代碼:
#region 自適應 private Size m_szInit;//初始窗體大小 private Dictionary<Control, Rectangle> m_dicSize = new Dictionary<Control, Rectangle>(); protected override void OnLoad(EventArgs e) { m_szInit = this.Size;//獲取初始大小 this.GetInitSize(this); base.OnLoad(e); } private void GetInitSize(Control ctrl) { foreach (Control c in ctrl.Controls) { m_dicSize.Add(c, new Rectangle(c.locatio{過濾}n, c.Size)); this.GetInitSize(c); } } protected override void OnResize(EventArgs e) { //計算當前大小和初始大小的比例 float fx = (float)this.Width / m_szInit.Width; float fy = (float)this.Height / m_szInit.Height; foreach (var v in m_dicSize) { v.Key.Left = (int)(v.Value.Left * fx); v.Key.Top = (int)(v.Value.Top * fy); v.Key.Width = (int)(v.Value.Width * fx); v.Key.Height = (int)(v.Value.Height * fy); } base.OnResize(e); } #endregion