实现组件大小随窗口大小变化

暂时只学会用代码的方式,如果后续了解到在可视化界面的属性栏中可以设置会进而更新分享

组件界面和一些属性

在这里插入图片描述
一些布局属性
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

详细代码实现

在这里插入图片描述

详细代码

//从这里开始复制下面的代码!!!
        //第一步,定义控件自适应窗口类AutoSizeFormClass
        class AutoSizeFormClass
        {
    
    
            public struct ControlRect
            {
    
    
                public int Left;
                public int Top;
                public int Width;
                public int Height;
            }

            public List<ControlRect> oldCtrl = new List<ControlRect>();
            int ctrlNo = 0;

            //重要方法一:记录窗体和其控件的初始位置和大小controllInitializeSize()
            public void controllInitializeSize(Control mForm)
            {
    
    
                ControlRect cR;
                cR.Left = mForm.Left; cR.Top = mForm.Top; cR.Width = mForm.Width; cR.Height = mForm.Height;
                oldCtrl.Add(cR);
                AddControl(mForm);

                foreach (ControlRect item in oldCtrl)
                {
    
    
                    Console.WriteLine(item.Width);
                }
            }
            private void AddControl(Control ctl)
            {
    
    
                foreach (Control c in ctl.Controls)
                {
    
    
                    ControlRect objCtrl;
                    objCtrl.Left = c.Left; objCtrl.Top = c.Top; objCtrl.Width = c.Width; objCtrl.Height = c.Height;
                    oldCtrl.Add(objCtrl);
                    if (c.Controls.Count > 0)
                        AddControl(c);
                }
            }

            //重要方法二:控件自适应大小方法controlAutoSize()
            public void controlAutoSize(Control mForm)
            {
    
    
                if (ctrlNo == 0)
                {
    
    
                    ControlRect cR;
                    cR.Left = 0; cR.Top = 0; cR.Width = mForm.PreferredSize.Width; cR.Height = mForm.PreferredSize.Height;
                    oldCtrl.Add(cR);
                    AddControl(mForm);
                }
                float wScale = (float)mForm.Width / (float)oldCtrl[0].Width;
                float hScale = (float)mForm.Height / (float)oldCtrl[0].Height;
                ctrlNo = 1;
                AutoScaleControl(mForm, wScale, hScale);
            }
            private void AutoScaleControl(Control ctl, float wScale, float hScale)
            {
    
    
                int ctrLeft0, ctrTop0, ctrWidth0, ctrHeight0;
                foreach (Control c in ctl.Controls)
                {
    
    
                    ctrLeft0 = oldCtrl[ctrlNo].Left;
                    ctrTop0 = oldCtrl[ctrlNo].Top;
                    ctrWidth0 = oldCtrl[ctrlNo].Width;
                    ctrHeight0 = oldCtrl[ctrlNo].Height;

                    c.Left = (int)((ctrLeft0) * wScale);
                    c.Top = (int)((ctrTop0) * hScale);//
                    c.Width = (int)(ctrWidth0 * wScale);
                    c.Height = (int)(ctrHeight0 * hScale);//
                    ctrlNo++;
                    if (c.Controls.Count > 0)
                        AutoScaleControl(c, wScale, hScale);

                    if (ctl is DataGridView)
                    {
    
    
                        DataGridView dgv = ctl as DataGridView;
                        Cursor.Current = Cursors.WaitCursor;

                        int widths = 0;
                        for (int i = 0; i < dgv.Columns.Count; i++)
                        {
    
    
                            dgv.AutoResizeColumn(i, DataGridViewAutoSizeColumnMode.AllCells);  // 自动调整列宽  
                            widths += dgv.Columns[i].Width;   // 计算调整列后单元列的宽度和                      
                        }
                        if (widths >= ctl.Size.Width)  // 如果调整列的宽度大于设定列宽  
                            dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;  // 调整列的模式 自动  
                        else
                            dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;  // 如果小于 则填充  

                        Cursor.Current = Cursors.Default;
                    }
                }
            }
        }


        //第二步,实例化上面定义的类
        AutoSizeFormClass asc = new AutoSizeFormClass();

        //第三步,初始化窗口时记录控件的大小和位置
        private void Auto_Load(object sender, EventArgs e)
        {
    
    
            asc.controllInitializeSize(this);
        }

        //第四步,主窗口大小发生变化触发的事件
        private void Auto_SizeChanged(object sender, EventArgs e)
        {
    
    
            asc.controlAutoSize(this);
        }

效果

在这里插入图片描述
放大到全屏的效果,(拖动窗口大小也会跟着变化)
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/munangs/article/details/132097970