C#控件大小随窗口变化?
- 提问者网友:练爱
- 2021-04-20 12:03
- 五星知识达人网友:北方的南先生
- 2021-04-20 13:41
- 1楼网友:舊物识亽
- 2021-04-20 16:19
- 2楼网友:長槍戰八方
- 2021-04-20 15:17
复制到你的窗体中,AutoScale(this);写在Form_Load()里。
public void AutoScale(Form frm) { frm.Tag = frm.Width.ToString() + "," + frm.Height.ToString(); frm.SizeChanged += new EventHandler(frm_SizeChanged); } public void frm_SizeChanged(object sender, EventArgs e) { string[] tmp = ((Form)sender).Tag.ToString().Split(','); float width = (float)((Form)sender).Width / (float)Convert.ToInt32(tmp[0]); float heigth = (float)((Form)sender).Height / (float)Convert.ToInt32(tmp[1]);
((Form)sender).Tag = ((Form)sender).Width.ToString() + "," + ((Form)sender).Height;
string str = ((Form)sender).Tag.ToString(); int font_size = Int32.Parse(str.Substring(0, str.IndexOf(','))) / 100;
foreach (Control control in ((Form)sender).Controls) { control.Font = new Font("宋体", font_size + 1); control.Scale(new SizeF(width, heigth)); } }