第一种:推荐
在窗体中加上如下代码即可实现,但窗体点击放大按钮时却不能改变控件大小。
private
Size beforeResizeSize
=
Size.Empty;
protected override void OnResizeBegin(EventArgs e)
{
base .OnResizeBegin(e);
beforeResizeSize = this .Size;
}
void OnResizeEnd(EventArgs e)
{
base .OnResizeEnd(e);
// 窗口resize之后的大小
Size endResizeSize = this .Size;
获得变化比例
float percentWidth = ( float )endResizeSize.Width / beforeResizeSize.Width;
float percentHeight = ( float )endResizeSize.Height / beforeResizeSize.Height;
foreach (Control control in this .Controls)
{
if (control is DataGridView)
continue ;
按比例改变控件大小
control.Width = ( int )(control.Width * percentWidth);
control.Height = ( int )(control.Height * percentHeight);
为了不使控件之间覆盖 位置也要按比例变化
control.Left = ( int )(control.Left * percentWidth);
control.Top = ( int )(control.Top * percentHeight);
}
}
protected override void OnResizeBegin(EventArgs e)
{
base .OnResizeBegin(e);
beforeResizeSize = this .Size;
}
void OnResizeEnd(EventArgs e)
{
base .OnResizeEnd(e);
// 窗口resize之后的大小
Size endResizeSize = this .Size;
获得变化比例
float percentWidth = ( float )endResizeSize.Width / beforeResizeSize.Width;
float percentHeight = ( float )endResizeSize.Height / beforeResizeSize.Height;
foreach (Control control in this .Controls)
{
if (control is DataGridView)
continue ;
按比例改变控件大小
control.Width = ( int )(control.Width * percentWidth);
control.Height = ( int )(control.Height * percentHeight);
为了不使控件之间覆盖 位置也要按比例变化
control.Left = ( int )(control.Left * percentWidth);
control.Top = ( int )(control.Top * percentHeight);
}
}
第二种:效果很差
在加载事件中写 AutoScale(this);
设置窗口控件随窗口大小改变而改变
public new void AutoScale(Form frm)
{
frm.Tag = frm.Width.ToString() + " , " + frm.Height.ToString();
frm.SizeChanged += new EventHandler(frm_SizeChanged);
}
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 height = ( float )((Form)sender).Height / ( 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;
public new void AutoScale(Form frm)
{
frm.Tag = frm.Width.ToString() + " , " + frm.Height.ToString();
frm.SizeChanged += new EventHandler(frm_SizeChanged);
}
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 height = ( float )((Form)sender).Height / ( 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;
也可使字体随之改变
float
tempWidth
=
0F;
float tempHeight = 0F;
in ((Form)sender).Controls)
{
continue ;
is TextBox)
{
tempHeight = height;
tempWidth = width;
}
is Button)
{
if ( this .WindowState == FormWindowState.Maximized)
tempHeight -= 0.4F ;
else
tempHeight += 0.2F ;
control.Scale( new Sizef(tempWidth,tempHeight));
}
else
{
control.Scale( new Sizef(width,height));
}
}
}
float tempHeight = 0F;
in ((Form)sender).Controls)
{
continue ;
is TextBox)
{
tempHeight = height;
tempWidth = width;
}
is Button)
{
if ( this .WindowState == FormWindowState.Maximized)
tempHeight -= 0.4F ;
else
tempHeight += 0.2F ;
control.Scale( new Sizef(tempWidth,tempHeight));
}
else
{
control.Scale( new Sizef(width,height));
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。