1. 程式人生 > >【2017-12-06】winfrom 窗體自啟最大化,控件自適應

【2017-12-06】winfrom 窗體自啟最大化,控件自適應

win con form width from int public osi convert

先將窗體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); } }

【2017-12-06】winfrom 窗體自啟最大化,控件自適應