天天看點

C# 控件随視窗大小變化自動縮放

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);
            }
        }