改用锚定的方式,通过与父级窗体的绑定来实现动态调整控件大小(Anchor属性)。代码实现方式仍有待思索。
——————————————————————————————————————————
C#小白,项目中一个母窗体和几个子窗体,子窗体在panel中填充。目前在实现窗体最大化的时候,遇到了以下问题:
1、母窗体最大化,然后切换子窗体,再恢复初始化时,子窗体尺寸失控。
2、反复最大化、初始化,随机出现子窗体尺寸失控。
有没有大佬懂得这方面的问题。
窗体尺寸变化代码来自某博客学习得来。
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)
{
string[] mytag = con.Tag.ToString().Split(new char[] { ':' });
float a = Convert.ToSingle(mytag[0]) * newx;
con.Width = (int)a;
a = Convert.ToSingle(mytag[1]) * newy;
con.Height = (int)(a);
a = Convert.ToSingle(mytag[2]) * newx;
con.Left = (int)(a);
a = Convert.ToSingle(mytag[3]) * newy;
con.Top = (int)(a);
Single currentSize = Convert.ToSingle(mytag[4]) * Math.Min(newx, newy);
con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
if (con.Controls.Count > 0)
{
setControls(newx, newy, con);
}
}
}
void Form1_Resize(object sender, EventArgs e)
{
Resize1();
// this.Text = this.Width.ToString() + " " + this.Height.ToString();
}
void Resize1()
{
if (this.Width == X1 && this.Height == Y1)
{
}
else
{
float newx = (this.Width) / X;
float newy = this.Height / Y;
setControls(newx, newy, this);
}
}



