C#開發窗體程序拉伸控件自適應

C#開發窗體程序拉伸控件自適應


一、新建一個C#窗體程序

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 Test_Form
{
    public partial class Form1 : Form
    {
		//定義X,y用於記錄窗體初始大小
        private float X;
        private float Y;

        public Form1()
        {
            InitializeComponent();
        }
        FormSetSelfAuto fa = new FormSetSelfAuto();
        private void Form1_Load(object sender, EventArgs e)
        {
            this.Resize += new EventHandler(Form1_Resize);
            fa._x = this.Width;
            fa._y = this.Height;
            fa.setTag(this);
        }
        //重繪窗體
        private void Form1_Resize(object sender, EventArgs e)
        {
            fa.form_Resize(this);
        }
    }
}

二、添加一個類: class FormSetSelfAuto

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

namespace Test_Form
{
    class FormSetSelfAuto
    {
        private float X;
        private float Y;
        public float _x
        {
            set { X = value; }
        }
        public float _y
        {
            set { Y = value; }
        }
        
        //獲取控件的width,height,left,top,字體的大小值,存放在控件的tag屬性中
        public 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);
                }
            }
        }
        //根據窗體大小調整控件大小 
        public void setControls(float newx, float newy, Control cons)
        {
            //遍歷窗體中的控件,重新設置控件的值
            foreach (Control con in cons.Controls)
            {
                //獲取控件tag屬性值,並分割後存儲字符串數組
                string[] mytag = con.Tag.ToString().Split(new char[] { ':' });
                float a = Convert.ToSingle(mytag[0]) * newx;//根據窗體縮放比例確定控件的寬度值
                con.Width = (int)a;
                a = Convert.ToSingle(mytag[1]) * newy;
                con.Height = (int)a;//高度

                a = Convert.ToSingle(mytag[2]) * newx;
                con.Left = (int)a;//左邊緣距離
                a = Convert.ToSingle(mytag[3]) * newy;
                con.Top = (int)a;//上邊緣距離
                Single currentSize = Convert.ToSingle(mytag[4]) * newy;
                con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
                if (con.Controls.Count > 0)
                {
                    setControls(newx, newy, con);
                }

            }
        }
        public void form_Resize(Form fr)
        {
            float newx = (fr.Width) / X;
            float newy = (fr.Height) / Y;
            setControls(newx, newy, fr);
            fr.Text = fr.Width.ToString() + " " + fr.Height.ToString();
        }
    }
}

此時,程序運行後拖動邊框,內部控件隨之拉伸。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章