1. 程式人生 > >控制元件縮放

控制元件縮放

 #region zhj-2018-11-19

        #region 控制元件縮放
        double formWidth;//窗體原始寬度
        double formHeight;//窗體原始高度
        double scaleX;//水平縮放比例
        double scaleY;//垂直縮放比例
        Dictionary<string, string> controlInfo = new Dictionary<string, string>();//控制元件中心Left,Top,控制元件Width,控制元件Height,控制元件字型Size
/// <summary> /// 獲取所有原始資料 /// </summary> protected void GetAllInitInfo(Control CrlContainer) { if (CrlContainer.Parent == this) { formWidth = Convert.ToDouble(CrlContainer.Width); formHeight = Convert.ToDouble(CrlContainer.Height); }
foreach (Control item in CrlContainer.Controls) { if (item.Name.Trim() != "") controlInfo.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 CrlContainer) { scaleX = (Convert.ToDouble(CrlContainer.Width) / formWidth); scaleY = (Convert.ToDouble(CrlContainer.Height) / formHeight); } private void ControlsChange(Control CrlContainer) { double[] pos = new double[5];//pos陣列儲存當前控制元件中心Left,Top,控制元件Width,控制元件Height,控制元件字型Size foreach (Control item in CrlContainer.Controls) { if (item.Name.Trim() != "") { if ((item as UserControl) == null && item.Controls.Count > 0) ControlsChange(item); string[] strs = controlInfo[item.Name].Split(','); for (int j = 0; j < 5; j++) { pos[j] = Convert.ToDouble(strs[j]); } double itemWidth = pos[2] * scaleX; 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); item.Height = Convert.ToInt32(itemHeight); item.Font = new Font(item.Font.Name, float.Parse((pos[4] * Math.Min(scaleX, scaleY)).ToString())); } } } protected override void OnSizeChanged(EventArgs e) { base.OnSizeChanged(e); if (controlInfo.Count > 0) { ControlsChangeInit(this.Controls[0]); ControlsChange(this.Controls[0]); } } #endregion //private void FormSCAB_MouseDown(object sender, MouseEventArgs e) //{ // int x = e.X; //相對form視窗的座標,客戶區座標 // int y = e.Y; // int x1 = Control.MousePosition.X;//相對顯示器,螢幕的座標 // int y1 = Control.MousePosition.Y; //} #endregion