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

时间:2015-01-10 16:44:21   收藏:0   阅读:1217

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了。

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!