calculate all controls size and position in the form when the form resize

控件尺寸调整方法
本文介绍了一种在Windows窗体应用程序中调整控件大小和位置的方法。通过计算控件的目标尺寸和位置,确保控件在窗口缩放时仍能保持正确的布局。此外,还提供了针对DataGridView控件的特定调整策略。
// class with calculate size and position of control.
class UIElement
    {
	// those properties get the Control current size, font, position 
        public Control Control { get; set; }
        public Size OriginalSize { get; set; }
        public Font OriginalFont { get; set; }
        public Point OriginalPosition { get; set; }

        public void AdjustSize(System.Drawing.Size originalSize, System.Drawing.Size destinationSize)
        {
            //we need to calculate destination size
            System.Drawing.Size size = new Size();
            size.Width = destinationSize.Width * OriginalSize.Width / originalSize.Width;
            size.Height = destinationSize.Height * OriginalSize.Height / originalSize.Height;

            Control.Size = size;

            //now lets caculate positions
            System.Drawing.Point position = new Point();
            position.X = destinationSize.Width * OriginalPosition.X / originalSize.Width;
            position.Y = destinationSize.Height * OriginalPosition.Y / originalSize.Height;

            Control.Location = position;

            Font font = new Font(OriginalFont.FontFamily, destinationSize.Height * OriginalFont.Size / originalSize.Height, OriginalFont.Style, OriginalFont.Unit);

            //adjusting font size
            Control.Font = font;

            if (Control is DataGridView)
            {
                DataGridView grid = (DataGridView)Control;
                grid.ColumnHeadersHeight = destinationSize.Height * 20 / originalSize.Height;
                grid.RowTemplate.Height = destinationSize.Height * 22 / originalSize.Height;
            }
        }
    }


 public partial class FormBase : Form
    {
        List<UIElement> UIElements { get; set; }

        public FormBase()
        {
            InitializeComponent();

            //we need to store ids of all controls found on the form
            UIElements = new List<UIElement>();
            AddControls(this.Controls);
        }

	// recursive function to load all child controls.
        public void AddControls(Control.ControlCollection controls)
        {
            foreach (Control control in controls)
            {
                UIElement element = new UIElement();
                element.Control = control;
                element.OriginalFont = control.Font;
                element.OriginalPosition = control.Location;
                element.OriginalSize = control.Size;
                UIElements.Add(element);

                if (control.Controls.Count > 0)
                    AddControls(control.Controls);
            }
        }

	// add key board command to window, "Ctrl + A"
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            switch (keyData)
            {
                case Keys.Control | Keys.A:
                    // Execute the Ctrl + C button action
                    ProgramValues.IsAlarm = true;
		
		// do some thing here ...
		// .......

                    return true;
                default:
                    break;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }


       // this function register to event ResizeEnd, may be also need called when the form load.
        internal void FormBase_ResizeEnd(object sender, EventArgs e)
        {
            Size original = new Size(472, 388);

            //Size original = ProgramValues.Original;

            //making sure that new size is not less than original size
            if (this.Size.Width < original.Width)
                this.Size = original;

            //scaling new size proportionally
            this.Size = new Size(this.Size.Width, this.Size.Width * original.Height / original.Width);

            //we need to adjust the size of all controls
            foreach (UIElement element in UIElements)
            {
                element.AdjustSize(original, this.Size);
            }

	// remember the size to some where.	
            ProgramValues.CurrentSize = new Size(this.Size.Width, this.Size.Height);

            if (this.Visible)
            {
                ProgramValues.CurrentLocation = new Point(this.Location.X, this.Location.Y);
            }

        }

    }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值