Window Form所有组件按主Form扩大

今天遇到了Form 扩大到问题,写下了如下代码,希望对大家有用

 

添加Event

            this.SizeChanged += new System.EventHandler(this.××_SizeChanged);

下面就是基本的代码

private ArrayList InitialCrl = new ArrayList();
        private ArrayList CrlLocationX = new ArrayList();
        private ArrayList CrlLocationY = new ArrayList();
        private ArrayList CrlSizeWidth = new ArrayList();
        private ArrayList CrlSizeHeight = new ArrayList();
        private int FormSizeWidth;
        private int FormSizeHeight;

        private double FormSizeChangedX;
        private double FormSizeChangedY;

        private int Wcounter = 0;

        private void ××_Load(object sender, EventArgs e)
        {
            GetInitialFormSize();
            GetAllCrlLocation(this);
            GetAllCrlSize(this);
        }

        public void GetAllCrlLocation(Control CrlContainer)
        {
            foreach (Control iCrl in CrlContainer.Controls)
            {

                if (iCrl.Controls.Count > 0)
                    GetAllCrlLocation(iCrl);
                InitialCrl.Add(iCrl);
                CrlLocationX.Add(iCrl.Location.X);
                CrlLocationY.Add(iCrl.Location.Y);


            }
        }
        public void GetAllCrlSize(Control CrlContainer)
        {
            foreach (Control iCrl in CrlContainer.Controls)
            {
                if (iCrl.Controls.Count > 0)
                    GetAllCrlSize(iCrl);
                CrlSizeWidth.Add(iCrl.Width);
                CrlSizeHeight.Add(iCrl.Height);
            }
        }
        public void GetInitialFormSize()
        {

            FormSizeWidth = this.Size.Width;
            FormSizeHeight = this.Size.Height;

        } 

        private void ××_SizeChanged(object sender, EventArgs e)
        {
            Wcounter = 0;
            int counter = 0;
            if (this.Size.Width < FormSizeWidth || this.Size.Height < FormSizeHeight)
            {
               
                foreach (Control iniCrl in InitialCrl)
                {
                    iniCrl.Width = (int)CrlSizeWidth[counter];
                    iniCrl.Height = (int)CrlSizeHeight[counter];
                    Point point = new Point();
                    point.X = (int)CrlLocationX[counter];
                    point.Y = (int)CrlLocationY[counter];
                    iniCrl.Bounds = new Rectangle(point, iniCrl.Size);
                    counter++;
                }
                this.AutoScroll = true;
            }
            else
            {
                this.AutoScroll = false;
                ResetAllCrlState(this);
            }
        }

        public void ResetAllCrlState(Control CrlContainer)
        { 

            FormSizeChangedX = (double)this.Size.Width / (double)FormSizeWidth;
            FormSizeChangedY = (double)this.Size.Height / (double)FormSizeHeight;
           
            foreach (Control kCrl in CrlContainer.Controls)
            {
                if (kCrl.Controls.Count > 0)
                {
                    ResetAllCrlState(kCrl);
                  
                }
                Point point = new Point();
                point.X = (int)((int)CrlLocationX[Wcounter] * FormSizeChangedX);
                point.Y = (int)((int)CrlLocationY[Wcounter] * FormSizeChangedY);
                kCrl.Width = (int)((int)CrlSizeWidth[Wcounter] * FormSizeChangedX);
                kCrl.Height = (int)((int)CrlSizeHeight[Wcounter] * FormSizeChangedY);
                kCrl.Bounds = new Rectangle(point, kCrl.Size);
                Wcounter++;

            }
        } 
 

你可能感兴趣的:(window)