100分!c#的关于Groupbox的问题里有控件
Groupbox的控件想随窗体变化,即窗体最大化的时候,所有空间一起变化,可是我写的代码只有GroupBox本身变大了,里面的空间还是原来的大小,代码如下:publicp...
Groupbox的控件想随窗体变化,即窗体最大化的时候,所有空间一起变化,可是我写的代码只有GroupBox本身变大了,里面的空间还是原来的大小,代码如下:
public partial class frmEmployee : Form
{
public frmEmployee()
{
InitializeComponent();
//控件随窗体变化
int count = this.Controls.Count * 2 + 2;
float[] factor = new float[count];
int i = 0;
factor[i++] = Size.Width;
factor[i++] = Size.Height;
foreach (Control ctrl in this.Controls)
{
factor[i++] = ctrl.Location.X / (float)Size.Width;
factor[i++] = ctrl.Location.Y / (float)Size.Height;
ctrl.Tag = ctrl.Size;
}
Tag = factor;
}
//控件随窗体变化
private void frmEmployee_Resize(object sender, EventArgs e)
{
float[] scale = (float[])Tag;
int i = 2;
foreach (Control ctrl in this.Controls)
{
ctrl.Left = (int)(Size.Width * scale[i++]);
ctrl.Top = (int)(Size.Height * scale[i++]);
ctrl.Width = (int)(Size.Width / (float)scale[0] * ((Size)ctrl.Tag).Width);
ctrl.Height = (int)(Size.Height / (float)scale[1] * ((Size)ctrl.Tag).Height);
//每次使用的都是最初始的控件大小,保证准确无误。
}
}
} 展开
public partial class frmEmployee : Form
{
public frmEmployee()
{
InitializeComponent();
//控件随窗体变化
int count = this.Controls.Count * 2 + 2;
float[] factor = new float[count];
int i = 0;
factor[i++] = Size.Width;
factor[i++] = Size.Height;
foreach (Control ctrl in this.Controls)
{
factor[i++] = ctrl.Location.X / (float)Size.Width;
factor[i++] = ctrl.Location.Y / (float)Size.Height;
ctrl.Tag = ctrl.Size;
}
Tag = factor;
}
//控件随窗体变化
private void frmEmployee_Resize(object sender, EventArgs e)
{
float[] scale = (float[])Tag;
int i = 2;
foreach (Control ctrl in this.Controls)
{
ctrl.Left = (int)(Size.Width * scale[i++]);
ctrl.Top = (int)(Size.Height * scale[i++]);
ctrl.Width = (int)(Size.Width / (float)scale[0] * ((Size)ctrl.Tag).Width);
ctrl.Height = (int)(Size.Height / (float)scale[1] * ((Size)ctrl.Tag).Height);
//每次使用的都是最初始的控件大小,保证准确无误。
}
}
} 展开
3个回答
展开全部
依照你的代码,我帮你改了下,你看看.
public partial class frmEmployee : Form
{
public frmEmployee()
{
InitializeComponent();
NewMethod(this);
}
private void NewMethod(Control ctrls)
{
//控件随窗体变化
int count = ctrls.Controls.Count * 2 + 2;
float[] factor = new float[count];
int i = 0;
factor[i++] = Size.Width;
factor[i++] = Size.Height;
foreach (Control ctrl in ctrls.Controls)
{
factor[i++] = ctrl.Location.X / (float)Size.Width; //记录当前容器X坐标
factor[i++] = ctrl.Location.Y / (float)Size.Height; //记录当前容器Y坐标
ctrl.Tag = ctrl.Size;
NewMethod(ctrl);
}
Tag = factor;
}
private void frmEmployee_Resize(object sender, EventArgs e)
{
Full(this);
}
private void Full(Control ctrls)
{
float[] scale = (float[])Tag;
int i = 2;
foreach (Control ctrl in ctrls.Controls)
{
ctrl.Left = (int)(Size.Width * scale[i++]); //容器X坐标
ctrl.Top = (int)(Size.Height * scale[i++]); //容器Y坐标
ctrl.Width = (int)(Size.Width / (float)scale[0] * ((Size)ctrl.Tag).Width); //Width比例 scale[0] same this.Width
ctrl.Height = (int)(Size.Height / (float)scale[1] * ((Size)ctrl.Tag).Height);//Height比例 scale[1] same this.Height
Full(ctrl);
}
}
}
容器是一层一层嵌套的,而且控件本身也是容器;按照递归的思路,你的想法就可以实现了.
public partial class frmEmployee : Form
{
public frmEmployee()
{
InitializeComponent();
NewMethod(this);
}
private void NewMethod(Control ctrls)
{
//控件随窗体变化
int count = ctrls.Controls.Count * 2 + 2;
float[] factor = new float[count];
int i = 0;
factor[i++] = Size.Width;
factor[i++] = Size.Height;
foreach (Control ctrl in ctrls.Controls)
{
factor[i++] = ctrl.Location.X / (float)Size.Width; //记录当前容器X坐标
factor[i++] = ctrl.Location.Y / (float)Size.Height; //记录当前容器Y坐标
ctrl.Tag = ctrl.Size;
NewMethod(ctrl);
}
Tag = factor;
}
private void frmEmployee_Resize(object sender, EventArgs e)
{
Full(this);
}
private void Full(Control ctrls)
{
float[] scale = (float[])Tag;
int i = 2;
foreach (Control ctrl in ctrls.Controls)
{
ctrl.Left = (int)(Size.Width * scale[i++]); //容器X坐标
ctrl.Top = (int)(Size.Height * scale[i++]); //容器Y坐标
ctrl.Width = (int)(Size.Width / (float)scale[0] * ((Size)ctrl.Tag).Width); //Width比例 scale[0] same this.Width
ctrl.Height = (int)(Size.Height / (float)scale[1] * ((Size)ctrl.Tag).Height);//Height比例 scale[1] same this.Height
Full(ctrl);
}
}
}
容器是一层一层嵌套的,而且控件本身也是容器;按照递归的思路,你的想法就可以实现了.
展开全部
即窗体最大化的时候,所有空间一起变化,可是我写的代码只有GroupBox本身变大了,里面的空间还是原来的大小.
其实原因很简单,
问题就出现在foreach (Control ctrl in this.Controls) 这里
this.Controls里并不包含GroupBox里的Control
所以,this.Controls 应该换成this.GroupBox.Controls
不过,foreach 里的循环还得改一下,你自己再研究下.
希望你能有所帮助
其实原因很简单,
问题就出现在foreach (Control ctrl in this.Controls) 这里
this.Controls里并不包含GroupBox里的Control
所以,this.Controls 应该换成this.GroupBox.Controls
不过,foreach 里的循环还得改一下,你自己再研究下.
希望你能有所帮助
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public partial class frmEmployee : Form
{
int ConstWidth, ConstHeight;
public frmEmployee()
{
InitializeComponent();
ConstWidth = Size.Width;
ConstHeight = Size.Height;
Read(this);
}
class Cont
{
public Size size;
public int left;
public int top;
}
private void Read(Control ctrls)
{
foreach (Control ctrl in ctrls.Controls)
{
Cont cont = new Cont();
cont.size = ctrl.Size;
cont.left = ctrl.Left;
cont.top = ctrl.Top;
ctrl.Tag = cont;
if (ctrls.Controls.Count > 0)
Read(ctrl);
}
}
private void frmEmployee_Resize(object sender, EventArgs e)
{
Full(this);
}
private void Full(Control ctrls)
{
foreach (Control ctrl in ctrls.Controls)
{
Cont cont=(Cont)ctrl.Tag;
ctrl.Left = (int)(((float)this.Size.Width / (float)ConstWidth) * (float)cont.left);
ctrl.Top = (int)(((float)this.Size.Height / (float)ConstHeight) * (float)cont.top);
ctrl.Width = (int)(((float)this.Size.Width / (float)ConstWidth) * (float)cont.size.Width);
ctrl.Height = (int)(((float)this.Size.Height / (float)ConstHeight) * (float)cont.size.Height);
if(ctrls.Controls.Count>0)
Full(ctrl);
}
}
}
这段代码是完整版 自己看看吧
{
int ConstWidth, ConstHeight;
public frmEmployee()
{
InitializeComponent();
ConstWidth = Size.Width;
ConstHeight = Size.Height;
Read(this);
}
class Cont
{
public Size size;
public int left;
public int top;
}
private void Read(Control ctrls)
{
foreach (Control ctrl in ctrls.Controls)
{
Cont cont = new Cont();
cont.size = ctrl.Size;
cont.left = ctrl.Left;
cont.top = ctrl.Top;
ctrl.Tag = cont;
if (ctrls.Controls.Count > 0)
Read(ctrl);
}
}
private void frmEmployee_Resize(object sender, EventArgs e)
{
Full(this);
}
private void Full(Control ctrls)
{
foreach (Control ctrl in ctrls.Controls)
{
Cont cont=(Cont)ctrl.Tag;
ctrl.Left = (int)(((float)this.Size.Width / (float)ConstWidth) * (float)cont.left);
ctrl.Top = (int)(((float)this.Size.Height / (float)ConstHeight) * (float)cont.top);
ctrl.Width = (int)(((float)this.Size.Width / (float)ConstWidth) * (float)cont.size.Width);
ctrl.Height = (int)(((float)this.Size.Height / (float)ConstHeight) * (float)cont.size.Height);
if(ctrls.Controls.Count>0)
Full(ctrl);
}
}
}
这段代码是完整版 自己看看吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询