根据电脑屏幕分辨率调整控件至最大化

public class MainForm

{ public float wX = Screen.PrimaryScreen.Bounds.Width; public float hY = Screen.PrimaryScreen.Bounds.Height; public MainForm() { InitializeComponent(); //switch screen float newx = wX / this.Width; float newy = hY / this.Height; setTag(this); setControls(newx, newy, this);

} private void setTag(Control cons) { foreach (Control con in cons.Controls) { con.Tag = con.Width + ":" + con.Height + ":" + con.Left + ":" + con.Top; if (con.Controls.Count > 0) { setTag(con); } } } private void setControls(float newx, float newy, Control cons) { foreach (Control con in cons.Controls) { string[] myTag = new string[5]; // MessageBox.Show(con.Tag.ToString()); myTag = con.Tag.ToString().Split(:); 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); } } }

}

经验分享 程序员 微信小程序 职场和发展