C#根據Form大小控件自動更改大小(自適應)(字體自適應)

C#From窗體改變後控件自適應代碼,可自動改變控件中的控件及字體大小

 

//長話短說 //大家互相交流,有不足處請指正

 

//此代碼設計思路:記錄初始的Form的寬度與高度,每次更改後算出比例,然後再等比例設置所有控件的Top,Left,Width,Heigh,及字體的大小更改

//字體大小更改關鍵字:自動更改字體的大小  //2020-03-23 19:21:50 更新

//可搜關鍵字://核心代碼

 

//整個源代碼如下:

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 FormResize

{

    public partial class Form1 : Form

    {

        // <summary>

        /// 窗體改變前的Width寬度

        /// </summary>

        private int iForm_ResizeBefore_Width;

        /// <summary>

        /// 窗體改變前的Height高度

        /// </summary>

        private int iForm_ResizeBefore_Height;

 

        /// <summary>

        /// 取得Form中控件中初始的字體大小,即無論Form多大此字體都是Form中最小值

        /// </summary>

        private float FontSizeMin = 0; //2020-03-23 19:21:50 更新

 

        /// <summary>

        /// 寬比例,float類型,此處爲每次改後窗體的寬度 除以改變前窗體的寬度的比例

        /// </summary>

        private float fRatio_Width;

        /// <summary>

        /// 高比例,float類型,此處爲每次改後窗體的高度 除以改變前窗體的高度的比例

        /// </summary>

        private float fRatio_Height;

 

        public Form1()

        {

            InitializeComponent();

 

            //代碼增加ResizeEnd事件   //也可通過設計器增加ResizeEnd事件然後刪掉此處代碼

            this.ResizeEnd += new System.EventHandler(this.Form1_ResizeEnd);

 

            //設置最小大小爲窗體打開的初始大小

            this.MinimumSize = this.Size;

 

            //取得Form中控件中初始的字體大小 //若此From中字體不統一,則此處爲數組。

            FontSizeMin = this.button1.Font.Size; //2020-03-23 19:21:50 更新

 

            //獲得窗體改變前的Form的寬度與高度 //賦初始值

            iForm_ResizeBefore_Width = this.Size.Width;

            iForm_ResizeBefore_Height = this.Size.Height;

        }

        private void Form1_ResizeEnd(object sender, EventArgs e) //窗體改變後發生的事件

        {

            //窗體改變後Form的寬度與高度及寬比例和高比例

            fRatio_Width = (this.Size.Width / (float)iForm_ResizeBefore_Width);

            fRatio_Height = (this.Size.Height / (float)iForm_ResizeBefore_Height);

            AutoResize(this); //2019-3-21 10:50:42更新

 

            ////獲得下一次窗體改變前的Form的寬度與高度

            iForm_ResizeBefore_Width = this.Size.Width;

            iForm_ResizeBefore_Height = this.Size.Height;

        }

 

        // <summary>

        /// 自適應窗體大小的方法,遞歸

        /// </summary>

        /// <param name="theControl"></param>

        private void AutoResize(Control theControl)  //核心代碼

        {

            #region 控制控件自適應UI大小

            foreach (Control OneCon in theControl.Controls)   //根據原大小等比例放大

            {

                OneCon.Left = Convert.ToInt32((OneCon.Left * fRatio_Width));

                OneCon.Top = Convert.ToInt32((OneCon.Top * fRatio_Height));

                OneCon.Width = Convert.ToInt32(OneCon.Width * fRatio_Width);

                OneCon.Height = Convert.ToInt32(OneCon.Height * fRatio_Height);

                if (OneCon.Controls.Count > 0)

                {

                    AutoResize(OneCon);//如Form內還有控件中的控件,此處則可遞歸改變大小,此處爲重點中的重點  //2019-3-21 10:50:42更新

                }

                #region 自動更改字體的大小  //2020-03-23 19:21:50 更新

                // 當前控件(OneCon.Font.Size) 乘以(8)  長寬比例中最小值 (Math.Min(fRatio_Width, fRatio_Height)

                //Math.Round(數值, 2) //保留兩位有效數字

                float currentSize = float.Parse(Math.Round(OneCon.Font.Size * (Math.Min(fRatio_Width, fRatio_Height)), 2).ToString());

                if (currentSize > FontSizeMin)

                    OneCon.Font = new Font(OneCon.Font.Name, currentSize, OneCon.Font.Style, OneCon.Font.Unit);

                else

                    OneCon.Font = new Font(OneCon.Font.Name, FontSizeMin, OneCon.Font.Style, OneCon.Font.Unit);

                //某種程度上來說 if 語句都可以改成三目運算符, 舉例: m=a>b?a:b; 表示先判斷a是否大於b,若a>b,則將a的值賦給m,若不符合a>b,則將b的值賦給m

                //float NowSize = currentSize > FontSizeMin ? currentSize : FontSizeMin;

                //OneCon.Font = new Font(OneCon.Font.Name, NowSize, OneCon.Font.Style, OneCon.Font.Unit);

                #endregion

            }

            #endregion

        }

    }

}

 

備註:1.Form顯示時可以調整到的最小的大小,代碼-> this.MinimumSize = this.Size; //2019-1-25 10:29:24更新

2.修改代碼 使之可以改變控件中的控件的大小//2019-3-21 20:50:42更新

3.新增變量FontSizeMin來確認自動更改字體的大小,//2020-03-23 19:21:50 更新

4.源代碼下載地址:(https://download.csdn.net/download/shengmingzaiyuxuexi/12264681)(或者私信我要源碼也可)

5.代碼可能不夠縝密,如何優化,請指出

 

 

 

如有錯誤,請指正

如有疑問,請留言

如想提攜,請私信

與君共勉,共同進步!

 

編譯環境:Visual studio 2012

編譯語言:C#

 

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