C#實現表單拖動時各個控制項同比自動放縮大小

來源:互聯網
上載者:User

標籤:控制項   c#   

實現方式主要是利用panel控制項為主題,對於每個控制項的大小位置和字型這幾個屬性進行記錄,

然後根據表單改變的大小同時放縮。


簡要步驟如下:

1 建立C#表單程式項目。

2  Panel放置到表單。

3  設定屬性dock為fill。

4  注意MinnumSize不能設定為0, 改成大於0都行。

public partial class FrmDemo : Form    {        double dFrmWidth;        double dFrmHeight;        double dZoomHorizon;        double dZoomVerticality;        Dictionary<string, string> dicControlsAttribute = new Dictionary<string, string>();        protected void GetAllInitiateContrlInfo(Control CrlContainer)        {            if (CrlContainer.Parent == this)            {                dFrmWidth = Convert.ToDouble(CrlContainer.Width);                dFrmHeight = Convert.ToDouble(CrlContainer.Height);            }            foreach (Control item in CrlContainer.Controls)            {                if (item.Name.Trim() != "")                    dicControlsAttribute.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)                    GetAllInitiateContrlInfo(item);            }        }        private void ChangeControlsInitiate(Control CrlContainer)        {            dZoomHorizon = (Convert.ToDouble(CrlContainer.Width) / dFrmWidth);            dZoomVerticality = (Convert.ToDouble(CrlContainer.Height) / dFrmHeight);        }                private void ChangeCurrentControlAttr(Control CrlContainer)        {            double[] dPosition = new double[5];            foreach (Control item in CrlContainer.Controls)            {                if (item.Name.Trim() != "")                {                    if ((item as UserControl) == null && item.Controls.Count > 0)                        ChangeCurrentControlAttr(item);                    string[] strs = dicControlsAttribute[item.Name].Split(',');                    for (int j = 0; j < 5; j++)                    {                        dPosition[j] = Convert.ToDouble(strs[j]);                    }                    double itemWidth = dPosition[2] * dZoomHorizon;                    double itemHeight = dPosition[3] * dZoomVerticality;                    item.Left = Convert.ToInt32(dPosition[0] * dZoomHorizon - itemWidth / 2);                    item.Top = Convert.ToInt32(dPosition[1] * dZoomVerticality - itemHeight / 2);                    item.Width = Convert.ToInt32(itemWidth);                    item.Height = Convert.ToInt32(itemHeight);                    //item.Font = new Font(item.Font.Name, float.Parse                    //((dPosition[4] * Math.Min(dZoomHorizon, dZoomVerticality)).ToString()));                    //字型也可以實現同比放縮。                     }            }        }        protected override void OnSizeChanged(EventArgs e)        {            base.OnSizeChanged(e);            if (dicControlsAttribute.Count > 0)            {                ChangeControlsInitiate(this.Controls[0]);                ChangeCurrentControlAttr(this.Controls[0]);            }        }         public FrmDemo()        {            InitializeComponent();            GetAllInitiateContrlInfo(this.Controls[0]);//建構函式裡面調用即可。        }}

5 效果測試






相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.