关于Winform中控件大小随窗体大小等比例缩放public Form1() { InitializeComponent(); #region 初始化控件缩放 x Width; y Height; setTag(this); #endregion } #region 控件大小随窗体大小等比例缩放 private float x;//定义当前窗体的宽度 private float y;//定义当前窗体的高度 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); } } } private void setControls(float newx, float newy, Control cons) { //遍历窗体中的控件重新设置控件的值 foreach (Control con in cons.Controls) { //获取控件的Tag属性值并分割后存储字符串数组 if (con.Tag ! null) { string[] mytag con.Tag.ToString().Split(new char[] { ; }); //根据窗体缩放的比例确定控件的值 con.Width Convert.ToInt32(System.Convert.ToSingle(mytag[0]) * newx);//宽度 con.Height Convert.ToInt32(System.Convert.ToSingle(mytag[1]) * newy);//高度 con.Left Convert.ToInt32(System.Convert.ToSingle(mytag[2]) * newx);//左边距 con.Top Convert.ToInt32(System.Convert.ToSingle(mytag[3]) * newy);//顶边距 Single currentSize System.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); } } } } private void Form1_Resize(object sender, EventArgs e) { //重置窗口布局 float newx (this.Width) / x; float newy (this.Height) / y; setControls(newx, newy, this); } #endregion这个代码在后续使用中又发现了一些问题如果在有子窗口的情况下打开最大化按钮就容易出现子窗口不适配父窗口的情况这个时候需要调整子窗口控件的Anchor属性小窗口调整前调整后Anchor属性基本作用概述在C#窗体程序设计中控件的Anchor属性用于设置该控件的边缘与窗体边缘之间的位置关系。当窗体大小发生变化时该属性决定了控件如何调整自己的位置和大小。可以设置的属性值共有四个属性值可以设置可以只设置其中一个也可以设置多个。Top: 控件与窗体顶部保持固定距离。Bottom: 控件与窗体底部保持固定距离。Left: 控件与窗体左侧保持固定距离。Right: 控件与窗体右侧保持固定距离。
c#关于Winform中控件大小随窗体大小等比例缩放
关于Winform中控件大小随窗体大小等比例缩放public Form1() { InitializeComponent(); #region 初始化控件缩放 x Width; y Height; setTag(this); #endregion } #region 控件大小随窗体大小等比例缩放 private float x;//定义当前窗体的宽度 private float y;//定义当前窗体的高度 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); } } } private void setControls(float newx, float newy, Control cons) { //遍历窗体中的控件重新设置控件的值 foreach (Control con in cons.Controls) { //获取控件的Tag属性值并分割后存储字符串数组 if (con.Tag ! null) { string[] mytag con.Tag.ToString().Split(new char[] { ; }); //根据窗体缩放的比例确定控件的值 con.Width Convert.ToInt32(System.Convert.ToSingle(mytag[0]) * newx);//宽度 con.Height Convert.ToInt32(System.Convert.ToSingle(mytag[1]) * newy);//高度 con.Left Convert.ToInt32(System.Convert.ToSingle(mytag[2]) * newx);//左边距 con.Top Convert.ToInt32(System.Convert.ToSingle(mytag[3]) * newy);//顶边距 Single currentSize System.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); } } } } private void Form1_Resize(object sender, EventArgs e) { //重置窗口布局 float newx (this.Width) / x; float newy (this.Height) / y; setControls(newx, newy, this); } #endregion这个代码在后续使用中又发现了一些问题如果在有子窗口的情况下打开最大化按钮就容易出现子窗口不适配父窗口的情况这个时候需要调整子窗口控件的Anchor属性小窗口调整前调整后Anchor属性基本作用概述在C#窗体程序设计中控件的Anchor属性用于设置该控件的边缘与窗体边缘之间的位置关系。当窗体大小发生变化时该属性决定了控件如何调整自己的位置和大小。可以设置的属性值共有四个属性值可以设置可以只设置其中一个也可以设置多个。Top: 控件与窗体顶部保持固定距离。Bottom: 控件与窗体底部保持固定距离。Left: 控件与窗体左侧保持固定距离。Right: 控件与窗体右侧保持固定距离。