1 要想控件随窗口大小变化自动缩放,就要重写Resize函数就可以实现了。
protected override void OnResizeEnd(EventArgs e) { base.OnResizeEnd(e); Size endSize = this.Size; float percentWidth = (float)endSize.Width / _beforeDialogSize.Width; float percentHeight = (float)endSize.Height / _beforeDialogSize.Height; foreach (Control control in this.Controls) { if (control is DataGridView) continue; //按比例改变控件大小 control.Width = (int)(control.Width * percentWidth); control.Height = (int)(control.Height * percentHeight); //为了不使控件之间覆盖 位置也要按比例变化 control.Left = (int)(control.Left * percentWidth); control.Top = (int)(control.Top * percentHeight); } }说明:
1 foreach中如果界面有Groupbox,就要再用一个foreach了。