zoukankan      html  css  js  c++  java
  • 控件随窗口大小而改变(来自小抽奖系统)

    一、在做小抽奖系统时,遇到了个问题,就是控件要随着窗口的放大,位置和大小也随着改变,在网上找了很多资料,都是修改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);
  • 相关阅读:
    23种设计模式全解析
    Dubbo
    存储过程——存储过程与函数(四)
    ADO.NET- 基础总结及实例介绍
    存储过程——存储过程与视图(三)
    存储过程——增删改(二)
    简易三层架构详解
    Ado.Net实现简易(省、市、县)三级联动查询,还附加Access数据
    存储过程——介绍(一)
    SqlBulkCopy批量写入25万条数据只需3s
  • 原文地址:https://www.cnblogs.com/evan-success/p/4828129.html
Copyright © 2011-2022 走看看