首页 > 技术文章 > winform 窗体大小变化时,如何设置使控件一起按照比例变大

goingforward 2015-12-15 16:26 原文

 public Form()
        {
            InitializeComponent();
            int count = this.Controls.Count * 2 + 2; 
            float[] factor = new float[count]; 
            int i = 0; 
            factor[i++] = Size.Width; 
            factor[i++] = Size.Height; 
            foreach (Control ctrl in this.Controls) 
            { 
                factor[i++] = ctrl.Location.X / (float)Size.Width; 
                factor[i++] = ctrl.Location.Y / (float)Size.Height; 
                ctrl.Tag = ctrl.Size; 
            } 
            Tag = factor;
        }
private void Form_Resize(object sender, EventArgs e)
    {
        float[] scale = (float[])Tag;
        int i = 2;
        foreach (Control ctrl in this.Controls)
        {
            ctrl.Left = (int)(Size.Width * scale[i++]);
            ctrl.Top = (int)(Size.Height * scale[i++]);
            ctrl.Width = (int)(Size.Width / (float)scale[0] * ((Size)ctrl.Tag).Width);
            ctrl.Height = (int)(Size.Height / (float)scale[1] * ((Size)ctrl.Tag).Height); 
        }
    }

摘自http://bbs.csdn.net/topics/350169743   laj0600310219。

推荐阅读