码迷,mamicode.com
首页 > Windows程序 > 详细

窗体内的控件随窗体的大小改变控件大小

时间:2018-07-04 18:48:50      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:top   int   foreach   send   idt   scale   i++   cal   init   

  public Form1()

        {

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



                //每次使用的都是最初始的控件大小,保证准确无误。

            }

        }

 

窗体内的控件随窗体的大小改变控件大小

标签:top   int   foreach   send   idt   scale   i++   cal   init   

原文地址:https://www.cnblogs.com/skyhorseyk/p/9264355.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!