C#中,让WINFORM中的控件适应分辨率的变化

 float x = 0, y = 0;

 private void FormHome_Load(object sender, EventArgs e)
        {
          
            x = this.Width;
            y = this.Height;
            setTag(this);

        }

 public void setTag( Control  obj)
        {
            foreach (Control con in obj.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  obj)
        {
            foreach (Control con in obj.Controls)
            {
                con.AutoSize = false;
                string[] mytag = con.Tag.ToString().Split(':');

                con.Width = (int)(float.Parse (mytag[0]) * newx);
                con.Height = (int)(float.Parse(mytag[1]) * newy);
                con.Left = (int)(float.Parse(mytag[2]) * newx);
                con.Top = (int)(float.Parse(mytag[3]) * newy);
                //计算字体缩放比例,缩放字体
                float currentSize;
                currentSize = newy * float.Parse (mytag[4]);
                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 FormHome_Resize(object sender, EventArgs e)
        {
             float newx=0,newy=0;
             newx  = this.Width / x;
             newy = this.Height / y;
             setControls(newx, newy, this);

        }

你可能感兴趣的:(object,String,C#,float,WinForm)