winform 控件大小隨着窗體自適應


3個方法:

     #region 控件縮放變量
        double formWidth;//窗體原始寬度
        double formHeight;//窗體原始高度
        double scaleX;//水平縮放比例
        double scaleY;//垂直縮放比例
        Dictionary<string, string> ControlsInfo = new Dictionary<string, string>();
        private PictureBox pictureBox_Combine_Down;
        private GroupBox groupBox22;//控件中心Left,Top,控件Width,控件Height,控件字體Size
        #endregion
   #region 改變控件大小 //獲取控件原始信息 protected void GetAllInitInfo(Control ctrlContainer) { //int tempWidth = Screen.PrimaryScreen.Bounds.Width / 5 * 4; //int tempHeight = Screen.PrimaryScreen.Bounds.Height / 5 * 4; 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 ControlsChangeInit(Control ctrlContainer) { //scaleX = (double)4 / 5; //scaleY = (double)4 / 5; scaleX = (Convert.ToDouble(ctrlContainer.Width) / formWidth); scaleY = (Convert.ToDouble(ctrlContainer.Height) / formHeight); } //窗體改變時修改控件大小 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()));//字體 } } } private void FormNewInfraredPicture_SizeChanged(object sender, EventArgs e) { if (sizeBool2) { if (ControlsInfo.Count > 0)//如果字典中有數據,即窗體改變 { ControlsChangeInit(this.Controls[0]);//表示pannel控件 ControlsChange(this.Controls[0]); } } //if (ControlsInfo.Count > 0)//如果字典中有數據,即窗體改變 //{ // ControlsChangeInit(this.Controls[0]);//表示pannel控件 // ControlsChange(this.Controls[0]); //} } #endregion

其中是窗體sizeChanged事件調用和構造函數開始記錄控件初始化信息;

在窗體上放一個面板,面板dock屬性為fill,而其他控件都建立在這個面板上;

注:1.在非開發環境的電腦上會出現改變分辨率出粗情況,

看其執行順序會發現,改變分辨率情況下,窗口自動改變,程序運行sizechanged事件會出錯,

僅需在初始化窗體時候屏蔽sizeChanged事件執行即可。

2.在非開發機上最小化也會報錯,所以在sizechanged事件執行時候屏蔽if (this.WindowState == FormWindowState.Minimized)


免責聲明!

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



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