C#Winform怎么让控件随着主界面大小变化

一、基础版本

1、新建一个winform桌面应用,从左边的工具箱中拖入三个控件:button、 listbox 、 monthCalendar1
C#Winform怎么让控件随着主界面大小变化_第1张图片


2、点击主界面,鼠标右键查看代码
在界面初始化函数:InitializeComponent();后面加入下面代码:

  public Form2()
        {
            InitializeComponent();

            int count = this.Controls.Count * 2 + 2;   
            float[] nature = new float[count];  
            int i = 0;
            nature[i++] = Size.Width;  
            nature[i++] = Size.Height;  
            foreach (Control ctrl in this.Controls)   
            {
                nature[i++] = ctrl.Location.X / (float)Size.Width;  
                nature[i++] = ctrl.Location.Y / (float)Size.Height; 
                ctrl.Tag = ctrl.Size;
            }
            Tag = nature; 
        }

3、回到主界面,点击闪电标志,找到主界面的所有事件,然后双击Resize右边空白,就生成了主界面的Resize事件。
C#Winform怎么让控件随着主界面大小变化_第2张图片


4、在Resize事件里面加入下面的代码:

 private void Form2_Resize(object sender, EventArgs e)
        {
            float[] percent = (float[])Tag;          
            int i = 2;
            foreach (Control ctrl in this.Controls)  
            {
                ctrl.Left = (int)(Size.Width * percent[i++]);  
                ctrl.Top = (int)(Size.Height * percent[i++]);  
                ctrl.Width = (int)(Size.Width / (float)percent[0] * ((Size)ctrl.Tag).Width);
                ctrl.Height = (int)(Size.Height / (float)percent[1] * ((Size)ctrl.Tag).Height);
            }
        }

5、主界面的这个属性设置如下,然后就点击运行,拖动界面自适应大小。
C#Winform怎么让控件随着主界面大小变化_第3张图片


说明:这种桌面控件自适应大小的方法具有通用性,可以批量处理控件,简单快捷。

以上内容结束!


二、无敌版本

以下为增强版本,几乎所有软件都使用,小小的傻瓜式操作大大的用处。

1、新建一个桌面应用,拖几个控件

C#Winform怎么让控件随着主界面大小变化_第4张图片

2、实现以下代码:

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

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            x = Width;
            y = Height;
            setTag(this);
        }

        private readonly float x; //定义当前窗体的宽度
        private readonly float y; //定义当前窗体的高度

        private void setTag(Control cons)
        {
            foreach (Control con in cons.Controls)
            {
                con.Tag = con.Width + ";" + con.Height + ";" + con.Left + ";" + con.Top + ";" + con.Font.Size;
                if (con.Controls.Count > 0) setTag(con);
            }
        }

        private void setControls(float newx, float newy, Control cons)
        {
            //遍历窗体中的控件,重新设置控件的值
            foreach (Control con in cons.Controls)
                //获取控件的Tag属性值,并分割后存储字符串数组
                if (con.Tag != null)
                {
                    var mytag = con.Tag.ToString().Split(';');
                    //根据窗体缩放的比例确定控件的值
                    con.Width = Convert.ToInt32(Convert.ToSingle(mytag[0]) * newx); //宽度
                    con.Height = Convert.ToInt32(Convert.ToSingle(mytag[1]) * newy); //高度
                    con.Left = Convert.ToInt32(Convert.ToSingle(mytag[2]) * newx); //左边距
                    con.Top = Convert.ToInt32(Convert.ToSingle(mytag[3]) * newy); //顶边距
                    var currentSize = Convert.ToSingle(mytag[4]) * newy; //字体大小                   
                    if (currentSize > 0) con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
                    con.Focus();
                    if (con.Controls.Count > 0) setControls(newx, newy, con);

                }
        }

        /// 
        /// 重置窗体布局
        /// 
        private void ReWinformLayout()
        {
            var newx = Width / x;
            var newy = Height / y;
            setControls(newx, newy, this);

        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            ReWinformLayout();
        }
    }
}

总结

基础版本可做学习使用,增强版本属于万能公式,几乎所有的winform程序都适用。

你可能感兴趣的:(#,C#,c#,开发语言,.net)