2020-12-03 控件自动适应窗体大小 c# 

控件自动适应窗体大小 c# 

1.初始时,保存所有的控件的位置大小,用name来保存,

2.在窗体resize的时候就遍历所有的控件,按照窗体和原来的比例来*

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 控件自动适应窗体大小
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            resize_all_controls.get_now_size(this.Controls, this);
        }        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            resize_all_controls.resize_all(this.Controls, this);
        }
    }
}

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 控件自动适应窗体大小
{
  public   class resize_all_controls
    {
        public static List <string>   controls_name = new List<string>();
        public static List<Rectangle>  controls_xywh = new List<Rectangle>();
        public static Size old_size = new Size();
        public static void get_now_size(Control.ControlCollection ct, Form form)
        {
            old_size = form.Size;
            get_all_size(ct);
        }
        static void get_all_size(Control.ControlCollection ct)
        {
            foreach (Control item in ct)
            {
                if (item is GroupBox)
                {
                    get_all_size(item.Controls);
                }
                if (item is Panel)
                {
                    get_all_size(item.Controls);
                }
                if (item is TabControl)
                {
                    get_all_size(item.Controls);
                }
                get_control_xywh(item);
            }
        }
       static  void get_control_xywh(Control control )
        {
            controls_name.Add(control.Name );
            int x = control.Location.X;
            int y = control.Location.Y ;
            int w = control.Width;
            int h = control.Height;
            controls_xywh.Add(new Rectangle (x,y,w,h));
        }
        public static void resize_all(Control.ControlCollection ct, Form form)
        {
            Size new_size = form.Size;
            double x =(double ) new_size.Width / (double)old_size.Width;
            double y =(double ) new_size.Height / (double)old_size.Height;
            set_all_size(ct,x ,y); 
        }
        public static void set_all_size(Control.ControlCollection ct, double x, double y)
        {
            foreach (Control item in ct)
            {
                if (item is GroupBox)
                {
                    set_all_size(item.Controls, x, y);
                }
                if (item is Panel)
                {
                    set_all_size(item.Controls, x, y);
                }
                if (item is TabControl)
                {
                    set_all_size(item.Controls, x, y);
                }
                set_control_whxy(item, x, y);
            }
        }
        static void set_control_whxy(Control item, double x, double y)
        {
            string item_name = item.Name;
            for (int i = 0; i < controls_name.Count; i++)
            {
                string name = controls_name[i];
                if (name == item_name)
                {
                    Point point = new Point((int)(controls_xywh[i].X  * x), (int)(controls_xywh[i].Y  * y));
                    item.Location = point;
                    item.Width = (int)(controls_xywh[i].Width  * x);
                    item.Height = (int)(controls_xywh[i].Height  * y);
                }
            }
        }
    }
  
}
 


版权声明:本文为qq_32066515原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。