C# WinForm 窗体及其控件的自适应

下载该实例
https://download.csdn.net/download/chbxgbin/13112870

使用方法:
1.把自适应的类整体复制到你的工程命名空间里,这样做,每个窗体均可使用。
2.声明自适应类实例。
3.为窗体添加SizeChanged事件,并在其方法Form1_SizeChanged中,首次记录窗体和其控件初始位置和大小,之后调用类的自适应方法,完成自适应。
4.Panel,DataGridView等控件同样适用。

窗体:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace AutoSizeFormsApp
{
    public partial class AutoSize : Form
    {
        readonly AutoSizeFormClass ASC = new AutoSizeFormClass(); //++++++++++声明自适应类实例

        public AutoSize()
        {
            InitializeComponent();
        }

        private void AutoSize_Load(object sender, EventArgs e)
        {
            ASC.ControlInitializeSize(this); //++++++++++调用自适应类的初始化方法,记录窗体和其控件的初始位置和大小
        }

        private void AutoSize_SizeChanged(object sender, EventArgs e)
        {
            ASC.ControlAutoSize(this); //++++++++++调用自适应类的初始化方法,完成自适应。
        }
    }
}

图1:默认大小
默认大小
图2:窗体最大化后的大小
窗体最大化后的大小
类:

using System.Windows.Forms;
using System.Collections.Generic;

namespace AutoSizeFormsApp
{
    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;     //1;
        
        public void ControlInitializeSize(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);
        }

        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 AddControl(Control Ctrl)
        {
            foreach (Control C in Ctrl.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);
            }
        }

        private void AutoScaleControl(Control Ctrl, float wScale, float hScale)
        {
            int CtrlLeft0, CtrlTop0, CtrlWidth0, CtrlHeight0;
            foreach (Control C in Ctrl.Controls)
            {
                CtrlLeft0 = OldCtrl[CtrlNo].Left;
                CtrlTop0 = OldCtrl[CtrlNo].Top;
                CtrlWidth0 = OldCtrl[CtrlNo].Width;
                CtrlHeight0 = OldCtrl[CtrlNo].Height;
                C.Left = (int)((CtrlLeft0) * wScale);
                C.Top = (int)((CtrlTop0) * hScale);
                C.Width = (int)((CtrlWidth0) * wScale);
                C.Height = (int)((CtrlHeight0) * hScale);
                CtrlNo++;
                if (C.Controls.Count > 0)
                    AutoScaleControl(C, wScale, hScale);
                if (Ctrl is DataGridView)
                {
                    DataGridView DGV = Ctrl 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 >= Ctrl.Size.Width)
                        DGV.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
                    else
                        DGV.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                    Cursor.Current = Cursors.Default;
                }
            }
        }
    }
}

下载该实例
https://download.csdn.net/download/chbxgbin/13112870

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值