控件大小和字体随窗体大小变化
2009-09-08 20:37
429 查看
为了避免窗体大小发生变化时控件布局出现尴尬的局面,你可能要设置控件的Anchor或者Dock属性,诶,不过这很头疼的,界面复杂时不好控制。还有另一种方法,布局控件,可是它也不是万能的了,怎么办呢?哈哈,别急,下面给你好的方法:
//记录控件的宽、高、控件左边缘与其所在容器左边缘间距、控件上边缘与其所在容器上边缘间距、控件的字体大小
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]) * newy;
con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
if (con.Controls.Count > 0)
{
setControls(newx, newy, con);
}
}
}
private void Form1_Resize(object sender, EventArgs e)
{
float newx = this.Width / X;
float newy = this.Height / Y;
//float newy = (this.Height - this.statusStrip1.Height) / (Y - y);
setControls(newx, newy, this);
}
float X=1, Y=1;
//float y = 0;
private void Form1_Load(object sender, EventArgs e)
{
X = this.Width;
Y = this.Height;
//y = this.statusStrip1.Height;
setTag(this);
}
OK,搞定了O(∩_∩)O~
//记录控件的宽、高、控件左边缘与其所在容器左边缘间距、控件上边缘与其所在容器上边缘间距、控件的字体大小
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]) * newy;
con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
if (con.Controls.Count > 0)
{
setControls(newx, newy, con);
}
}
}
private void Form1_Resize(object sender, EventArgs e)
{
float newx = this.Width / X;
float newy = this.Height / Y;
//float newy = (this.Height - this.statusStrip1.Height) / (Y - y);
setControls(newx, newy, this);
}
float X=1, Y=1;
//float y = 0;
private void Form1_Load(object sender, EventArgs e)
{
X = this.Width;
Y = this.Height;
//y = this.statusStrip1.Height;
setTag(this);
}
OK,搞定了O(∩_∩)O~
相关文章推荐
- C#中怎样让控件大小随着窗体大小变化而变化?
- 窗体控件大小随窗体大小变化而变化
- C#巧用anchor和dock设计复杂界面(控件随着窗体大小的变化而变化)
- C#中怎样使控件随着窗体一起变化大小(常见困难以及修正)
- 让对话框内的控件在窗体大小发生变化时随之变化
- C# 巧用anchor和dock设计复杂界面(控件随着窗体大小的变化而变化)【转】
- Visual Studio 设置窗体控件,随窗体拖动变化大小
- C#窗体控件大小随窗体变化而变化的一种改进方法
- 窗体中控件自动随窗体变化大小
- CFormView上让控件的大小随着窗体的变化而自动适应
- MFC控件随窗体大小变化的实现
- CFormView上让控件的大小随着窗体的变化而自动适应
- Winform 控件随窗体大小改变而变化
- 【抽奖系统】-窗体控件跟随窗体的大小而变化-源码
- vs2013 窗体应用编程—控件大小随窗体变化(通过代码实现)
- 窗体的控件随窗体变化自动调整大小
- 窗体控件大小随窗体大小变化而变化(VB.NET)
- 控件字体大小随内容变化而变化
- MFC中让控件随窗体大小变化而变化中需要注意的问题
- 窗体控件大小随窗体大小变化而变化(VB.NET)