【2017-12-06】winfrom 窗体自启最大化,控件自适应

2021-04-23 17:59

阅读:357

先将窗体windowstate属性设置为Maximized    

public partial class Form1 : Form { public Form1() { InitializeComponent(); } //获取所有控件属性(长、宽、位置、字体大小) 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); label1.Text += con.Tag; } } //设置控件动态大小 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]) * Math.Min(newx, 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 Form1_Load(object sender, EventArgs e) { setTag(this); float newx = (float)this.Width / 794; //窗体制作时的宽度 float newy = (float)this.Height / 474; //窗体制作时的高度 label1.Text = newx.ToString(); setControls(newx, newy, this); } }


评论


亲,登录后才可以留言!