C# winform中 窗體縮放自適應的方法(不同電腦/不同分辨率)


      窗體縮放是一個困擾我多時的問題,為了解決這個問題,我從網上找了很多相關的資料,很多人說用Anchor和Dock屬性,但是我試了以后,始終不能達到想要的效果。

  后來,搜到了一個帖子,終於解決了這個問題,再次對該貼作者表示感謝。原帖鏈接為:

  http://www.cnblogs.com/sydeveloper/archive/2013/01/29/2881521.html

  以下是筆者的實現過程:

  1.  首先在窗體上放上一個Panel容器,並將容器的Dock屬性設為Fill,即所有的控件都放在了這個容器里。

  2. 設置縮放窗體時需要用到的變量:    

        #region 控件縮放
        double formWidth;//窗體原始寬度
        double formHeight;//窗體原始高度
        double scaleX;//水平縮放比例
        double scaleY;//垂直縮放比例
        Dictionary<string, string> ControlsInfo = new Dictionary<string, string>();//控件中心Left,Top,控件Width,控件Height,控件字體Size

        #endregion

  3.  自定義幾個方法,用以實現(1)獲取控件初始信息;GetAllInitInfo()

                (2)獲取窗體縮放比例;ControlsChaneInit()

                (3)窗體改變時修改控件大小。ControlsChange()

  

        protected void GetAllInitInfo(Control ctrlContainer)
        {
            if (ctrlContainer.Parent == this)//獲取窗體的高度和寬度
            {
                formWidth = Convert.ToDouble(ctrlContainer.Width);
                formHeight = Convert.ToDouble(ctrlContainer.Height);
            }
            foreach (Control item in ctrlContainer.Controls)
            {
                if (item.Name.Trim() != "")
                {
                    //添加信息:鍵值:控件名,內容:據左邊距離,距頂部距離,控件寬度,控件高度,控件字體。
                    ControlsInfo.Add(item.Name, (item.Left + item.Width / 2) + "," + (item.Top + item.Height / 2) + "," + item.Width + "," + item.Height + "," + item.Font.Size);
                }
                if ((item as UserControl) == null && item.Controls.Count > 0)
                {
                    GetAllInitInfo(item);
                }
            }

        }
        private void ControlsChaneInit(Control ctrlContainer)
        {
            scaleX = (Convert.ToDouble(ctrlContainer.Width) / formWidth);
            scaleY = (Convert.ToDouble(ctrlContainer.Height) / formHeight);
        }
        /// <summary>
        /// 改變控件大小
        /// </summary>
        /// <param name="ctrlContainer"></param>
        private void ControlsChange(Control ctrlContainer)
        {
            double[] pos = new double[5];//pos數組保存當前控件中心Left,Top,控件Width,控件Height,控件字體Size
            foreach (Control item in ctrlContainer.Controls)//遍歷控件
            {
                if (item.Name.Trim() != "")//如果控件名不是空,則執行
                {
                    if ((item as UserControl) == null && item.Controls.Count > 0)//如果不是自定義控件
                    {
                        ControlsChange(item);//循環執行
                    }
                    string[] strs = ControlsInfo[item.Name].Split(',');//從字典中查出的數據,以‘,’分割成字符串組

                    for (int i = 0; i < 5; i++)
                    {
                        pos[i] = Convert.ToDouble(strs[i]);//添加到臨時數組
                    }
                    double itemWidth = pos[2] * scaleX;     //計算控件寬度,double類型
                    double itemHeight = pos[3] * scaleY;    //計算控件高度
                    item.Left = Convert.ToInt32(pos[0] * scaleX - itemWidth / 2);//計算控件距離左邊距離
                    item.Top = Convert.ToInt32(pos[1] * scaleY - itemHeight / 2);//計算控件距離頂部距離
                    item.Width = Convert.ToInt32(itemWidth);//控件寬度,int類型
                    item.Height = Convert.ToInt32(itemHeight);//控件高度
                    item.Font = new Font(item.Font.Name, float.Parse((pos[4] * Math.Min(scaleX, scaleY)).ToString()));//字體

                }
            }

        }

 

  4.  在窗體類的構造函數中調用獲取初始數據的方法:

     #region 窗體縮放
            GetAllInitInfo(this.Controls[0]);
            #endregion

 

  5.  在窗體的尺寸改變的響應事件中加入方法:

    

        private void frmMain_SizeChanged(object sender, EventArgs e)
        {
            if (ControlsInfo.Count > 0)//如果字典中有數據,即窗體改變
            {
                ControlsChangeInit(this.Controls[0]);//表示pannel控件
                ControlsChange(this.Controls[0]);

            }
        }

 

  經過上述幾個步驟,即可實現窗體的自由縮放,它的基本思想是:通過對控件的尺寸進行重置來實現窗體的自適應。即,先將原窗體的初始值進行存儲,然后通過SizeChange事件響應窗體改變,實現重繪。

  需要注意的是:獲取初始值的方法,應該放在構造函數中,而不是Load事件中,這個問題導致我繞了個彎。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM