控件自适应大小【C#】

1、抽象出自适应大小的类为:

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

namespace FormAutoSize
{
    class AutomaticSize
    {
        /*
             在需要自适应大小的窗体中添加如下事件
             *如果是容器,最好将Anchor和Dock属性修改过来.

             private AutomaticSize automaticSize = new AutomaticSize();

             private void FrmPwd_Load(object sender, EventArgs e)
             {
                 automaticSize.Width = this.Width;
                 automaticSize.Height = this.Height;
                 automaticSize.SetTag(this);
             }

             private void FrmPwd_Resize(object sender, EventArgs e)
             {
                 automaticSize.ControlResize(this);
             }
        
             */

        private float width;
        private float height;

        public float Height
        {
            get { return height; }
            set { height = value; }
        }

        public float Width
        {
            get { return width; }
            set { width = value; }
        }
        /// <summary>
        /// 设置Tag标签
        /// </summary>
        /// <param name="controls">控件</param>
        public void SetTag(Control controls)
        {
            foreach (Control control in controls.Controls)
            {
                control.Tag = control.Width + ":" + control.Height + ":" + control.Left + ":" + control.Top + ":" + control.Font.Size;
                if (control.Controls.Count > 0)
                {
                    SetTag(control);
                }
            }
        }
        /// <summary>
        /// 设置控件大小
        /// </summary>
        /// <param name="newX">X坐标</param>
        /// <param name="newY">Y坐标</param>
        /// <param name="controls">控件</param>
        private void SetControls(float newX, float newY, Control controls)
        {
            foreach (Control control in controls.Controls)
            {
                string[] myTag = control.Tag.ToString().Split(':');
                //控件的宽
                float length = Convert.ToSingle(myTag[0]) * newX;
                control.Width = (int)length;
                //控件的高
                length = Convert.ToSingle(myTag[1]) * newY;
                control.Height = (int)length;
                //控件的X坐标
                length = Convert.ToSingle(myTag[2]) * newX;
                control.Left = (int)length;
                //控件的Y坐标
                length = Convert.ToSingle(myTag[3]) * newY;
                control.Top = (int)length;

                Single currentSize = Convert.ToSingle(myTag[4]) * newY;

                control.Font = new System.Drawing.Font(control.Font.Name, currentSize, control.Font.Style, control.Font.Unit);

                if (control.Controls.Count > 0)
                {
                    SetControls(newX, newY, control);
                }

            }
        }
        /// <summary>
        /// 调整控件的大小
        /// </summary>
        public void ControlResize(Control control)
        {
            float newX = control.Width / this.width;
            float newY = control.Height / this.height;
            SetControls(newX, newY, control);
        }

    }
}


2、在需要自适应大小的Form界面,在load和resize方法中应用:

 

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

namespace FormAutoSize
{
    public partial class Form1 : Form
    {
        private AutomaticSize automaticSize = new AutomaticSize();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            automaticSize.Width = this.Width;
            automaticSize.Height = this.Height;
            automaticSize.SetTag(this);
        }
        private void Form1_Resize(object sender, EventArgs e)
        {
            automaticSize.ControlResize(this);
        }


    }

}


  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值