控件隨窗口大小而改變(來自小抽獎系統)

1、在作小抽獎系統時,遇到了個問題,就是控件要隨着窗口的放大,位置和大小也隨着改變,在網上找了不少資料,都是修改Anchor和Dock屬性值,但不符合我想要的效果;皇天不負苦心人啊,最後終於讓我找到了(以下)ide

 private float X, Y;
        private void setTag(Control cons)
        {
            foreach (Control con in cons.Controls)
            {
                con.Tag = con.Width + ":" + con.Height + ":" + con.Left + ":" + con.Top + ":" + con.Font.Size;
                if (con.Controls.Count > 0)
                    setTag(con);
            }
        }

        private void setControls(float newx, float newy, Control cons)
        {
            foreach (Control con in cons.Controls)
            {

                string[] mytag = con.Tag.ToString().Split(new char[] { ':' });
                float a = Convert.ToSingle(mytag[0]) * newx;
                con.Width = (int)a;
                a = Convert.ToSingle(mytag[1]) * newy;
                con.Height = (int)(a);
                a = Convert.ToSingle(mytag[2]) * newx;
                con.Left = (int)(a);
                a = Convert.ToSingle(mytag[3]) * newy;
                con.Top = (int)(a);
                Single currentSize = Convert.ToSingle(mytag[4]) * newy;
                con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
                if (con.Controls.Count > 0)
                {
                    setControls(newx, newy, con);
                }
            }

        }

        private void FrmClinicalTV_Load(object sender, EventArgs e)
        {
            this.Resize += new EventHandler(FrmClinicalTV_Resize);

            X = this.Width;
            Y = this.Height;
            //   y = this.statusStrip1.Height;
            setTag(this);
        }

        private void FrmClinicalTV_Resize(object sender, EventArgs e)
        {
            // throw new Exception("The method or operation is not implemented.");  
            float newx = (this.Width) / X;
            //  float newy = (this.Height - this.statusStrip1.Height) / (Y - y);  
            float newy = this.Height / Y;
            setControls(newx, newy, this);
            //this.Text = this.Width.ToString() + " " + this.Height.ToString();
        }
View Code

2、在啓動winfrom窗體時,界面會出現閃爍的狀況,這就會影響用戶的體驗感了,加入一段代碼就能夠減小這種狀況的出現this

 //減小閃爍
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
相關文章
相關標籤/搜索