你的位置:首页 > ASP.net教程

[ASP.net教程]控件随窗口大小而改变(来自小抽奖系统)

一、在做小抽奖系统时,遇到了个问题,就是控件要随着窗口的放大,位置和大小也随着改变,在网上找了很多资料,都是修改Anchor和Dock属性值,但不符合我想要的效果;皇天不负苦心人啊,最后终于让我找到了(如下)

 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

二、在启动winfrom窗体时,界面会出现闪烁的情况,这就会影响用户的体验感了,加入一段代码就可以减少这种情况的出现

 //减少闪烁      this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);