C#Winform控制項隨表單縮放

來源:互聯網
上載者:User

實現步驟:

1.在表單中放一個容器(例如:Panel),並且將容器的Dock屬性設定為Fill。表單中其他控制項都放在這個容器中。

2.建立一個表單類,該類繼承於原始表單類,並在建立的這個表單類中添加如下代碼,以後建立的表單都繼承於建立的這個表單類:

#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()));
        }
    }
}

#endregion 

3.在建立的表單類中重寫OnSizeChanged事件,並調用ControlsChangeInit和ControlsChange方法,代碼如下:

protected override void OnSizeChanged(EventArgs e)
{
    base.OnSizeChanged(e);
    if (controlInfo.Count > 0)
    {
        ControlsChangeInit(this.Controls[0]);
        ControlsChange(this.Controls[0]);
    }
}

4.在表單的建構函式中調用GetAllInitInfo方法,代碼如下:

    GetAllInitInfo(this.Controls[0]);

 

註:原創,轉載請指明出處。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.