日期:2014-05-17 浏览次数:20923 次
分三步:
1.控件必须放在一个panel中(可能其他也行,没试。。。);
2.from类中添加一个函数,并添加form的sizechanged事件,如下:
public void AutoResize(Form frm)
{
frm.Tag = frm.Width.ToString() + "," + frm.Height.ToString();
frm.SizeChanged += new EventHandler(Form1_SizeChanged);
}
事件如下:
private void Form1_SizeChanged(object sender, EventArgs e)
{
string[] tmp = ((Form)sender).Tag.ToString().Split(',');
float width = (float)((Form)sender).Width / (float)Convert.ToInt16(tmp[0]);
float heigth = (float)((Form)sender).Height / (float)Convert.ToInt16(tmp[1]);
((Form)sender).Tag = ((Form)sender).Width.ToString() + "," + ((Form)sender).Height;
foreach (Control control in ((Form)sender).Controls)
{
control.Scale(new SizeF(width, heigth));
}
}
3.在构造函数中调用AutoResize函数,如下:
public Form1()
{
InitializeComponent();
AutoResize(this);//函数调用
}