C#中怎樣使控件隨着窗體一起變化大小(常見困難以及修正)[轉]


這個是多么古老的話題啊,呵呵,可惜網上的解決方案都不大好。都是針對VB或者使用Dock和Anchor屬性的。我實在看不過去,所以自己總結了一下。

1.使用一些布局面板,比如FlowLayoutPanel,TabLayOutPanel之類,但是缺點就是樣式太死了。所以本人不采用。

2.采用Dock和Anchor屬性。這類雖然鼠標點幾下在屬性欄設置一下就好,但是缺乏靈活性。只有上下左右中間幾種選擇。

3.采用Form_Resize()。這種方法最靈活,思路是窗體變化時,直接重寫這個變化函數。

那么如何寫呢?第一要將原來窗體的屬性以及各個控件的所占位置的比例存入Tag中(Tag可以存任何東西哦!)

廢話少說,直接貼代碼:

public Form1()
        {
            InitializeComponent();
            int count = this.Controls.Count * 2+2;
            float[] factor = new float[count];
            int i = 0;
            factor[i++] = Size.Width;
            factor[i++] = Size.Height;
            foreach(Control ctrl in this.Controls)
            {
                factor[i++] = ctrl.Location.X / (float)Size.Width;
                factor[i++] = ctrl.Location.Y / (float)Size.Height;
            }
            Tag = factor;
        }//在初始化的時候將一切窗體位置的參數存入Tag

private void Form1_Resize(object sender, EventArgs e)
        {
            float[] scale = (float[])Tag;
            int i = 2;

            foreach (Control ctrl in this.Controls)
            {
                ctrl.Left = (int)(Size.Width * scale[i++]);
                ctrl.Top = (int)(Size.Height * scale[i++]);
                ctrl.Width = (int)(Size.Width / (float)scale[0] * ctrl.Width);
                ctrl.Height = (int)(Size.Height / (float)scale[1] * ctrl.Height);

            }
            scale[0] = Size.Width;
            scale[1] = Size.Height;
            Tag = scale;
        }//改寫大小

上面的代碼似乎沒有問題,但是實際上,每次控件大小的變化有誤差,導致多次變化后控件嚴重變型,紅色的代碼部分存在嚴重問題。那么應該如何修改呢?

經過本人的修改與測試,下面的代碼准確無誤。

      

  public Form1()
        {
            InitializeComponent();
            int count = this.Controls.Count * 2+2;
            float[] factor = new float[count];
            int i = 0;
            factor[i++] = Size.Width;
            factor[i++] = Size.Height;
            foreach(Control ctrl in this.Controls)
            {
                factor[i++] = ctrl.Location.X / (float)Size.Width;
                factor[i++] = ctrl.Location.Y / (float)Size.Height;
                ctrl.Tag = ctrl.Size;//!!!
            }
            Tag = factor;
        }

        private void Form1_Resize(object sender, EventArgs e)//方法在Form事件當中進行關聯
        {
            float[] scale = (float[])Tag;
            int i = 2;

            foreach (Control ctrl in this.Controls)
            {
                ctrl.Left = (int)(Size.Width * scale[i++]);
                ctrl.Top = (int)(Size.Height * scale[i++]);
             ctrl.Width = (int)(Size.Width / (float)scale[0] * ((Size)ctrl.Tag).Width);//!!! ctrl.Height = (int)(Size.Height / (float)scale[1] * ((Size)ctrl.Tag).Height);//!!!

                //每次使用的都是最初始的控件大小,保證准確無誤。
            }
        }

  

4.注意點和誤區。千萬不要使用Scale這個函數,這個函數很誘人,似乎可以改變控件大小,這個函數用了SizeF這個結構,然而第一改變比如最大化后是對的,但是你還原后一切都不是那么回事了。我也不知道是怎么回事。程序反正是不對的。如果有朋友使用過可以告訴我,謝謝。

5.還有人使用resize32.ocx這個控件。這個控件.net框架似乎不存在。所以不加以評論


免責聲明!

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



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