用Doc属性,设定控件的相对位置,当Doc属性为Fill时,此控件就会自动适应容器的大小
例如
|Button1 Doc=Left|Button2 Doc=Left|TextBox1 Doc=fill|这样的布局,Button1和2的大小是固定的,而TextBox1的大小就是随着它的父容器的大小而自动调整的
例如
|Button1 Doc=Left|Button2 Doc=Left|TextBox1 Doc=fill|这样的布局,Button1和2的大小是固定的,而TextBox1的大小就是随着它的父容器的大小而自动调整的
{
if(Convert.ToDecimal(this.Width)/Convert.ToDecimal(tempWidth)==1 &&
Convert.ToDecimal(this.Height)/Convert.ToDecimal(tempHeight)==1)
{
return;
}
else
{
Decimal fl =Convert.ToDecimal(this.Width)/Convert.ToDecimal(tempWidth);
Decimal fh=Convert.ToDecimal(this.Height)/Convert.ToDecimal(tempHeight);
foreach(Control cmicCtl in this.Controls)
{
cmicCtl.Left=Convert.ToInt32(cmicCtl.Left*fl);
cmicCtl.Width=Convert.ToInt32(cmicCtl.Width*fl);
cmicCtl.Top=Convert.ToInt32(cmicCtl.Top*fh);
}
tempHeight=this.Height;
tempWidth=this.Width;
}
}
private int tempWidth=787;//初始化窗体宽度