您的位置:首页 > 编程语言 > C#

C# 控件随窗口大小变化自动缩放

2017-05-16 11:30 2186 查看
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了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  control continue