C# 控件相对于Form位置不变
- 保存控件的原先坐标
private void ResizeInit(Form fr)
{
foreach (Control ctrl in fr.Controls)
{
ctrl.Tag = ctrl.Left.ToString()+" "+ctrl.Top.ToString();
}
}
- 计算Form改变前与改变后的比例
Form的原先大小 |
fPreSize[0] = (float)this.Width;
fPreSize[1] = (float)this.Height;
改变Form的大小 |
this.WindowState = FormWindowState.Maximized;
this.Width = Screen.PrimaryScreen.WorkingArea.Width;
this.Height = Screen.PrimaryScreen.WorkingArea.Height;
Form改变的比例 |
fNowSize[0] = (float)this.Width / fPreSize[0];
fNowSize[1] = (float)this.Height / fPreSize[1];
3.改变控件位置
private void ResizeForm(Form fr)
{
int left, top;
foreach (Control ctrl in fr.Controls)
{
string [] sp = new string[1]{" "};
string [] pos =((string)ctrl.Tag).Split(sp, StringSplitOptions.RemoveEmptyEntries);
left = Convert.ToInt32(pos[0]);
top = Convert.ToInt32(pos[1]);
ctrl.Left = (int)(left * fNowSize[0]);
ctrl.Top = (int)(top * fNowSize[1]);
}
}
把1放到Form的构造函数中
把2放到Form_Load中
把3也放到Form_Load中