窗口中的控件问题 50分 设计的是一个非最大化的窗口,要求最大化后,控件大小随之改变,不用 通过设置控件的Anchor属性的方法(panel设置Anchor属性就不能正常显示),需要各位提供的代码提示,谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 panel设置Anchor属性就不能正常显示-----------不懂,panel还有这个问题吗? Anchor是最好的解决办法,如果你自己写也和Anchor效果一样的。 僅供參考:http://blog.csdn.net/loveme1204/archive/2007/12/24/1964577.aspx 但是我设置panel的Anchor,显示的位置也不对啊 ,还有如果picturebox也这么设置,就更不对了啊 在窗体的resize事件中写代码啊,调整控件的大小就可以了 可以通过使用tablelayerpanel和控件的dock属性来实现,不过比Anchor要费很多事啊。 4樓結合這個網站:http://labs.developerfusion.co.uk/convert/csharp-to-vb.aspx翻譯不一定很准,你稍微微調下。經測試可用。 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 ); } } } void Form1_Resize(object sender, EventArgs e) { // throw new Exception("The method or operation is not implemented."); float newx = (this.Width )/ X; // float newy = (this.Height - this.statusStrip1.Height) / (Y - y); float newy = this.Height / Y; setControls(newx, newy, this); this.Text = this.Width.ToString() +" "+ this.Height.ToString(); }翻译的一篇BLOG,但是注意statusStrip1之类的,自己修改把.form_load中添加 this.Resize += new EventHandler(Form1_Resize); X = this.Width; Y = this.Height; y = this.statusStrip1.Height; setTag (this); public float X; public float Y; public float y; 为什么手动拉放窗口却不能用呢?Anchor不会缩放字体,而14楼的会缩放字体,不错... 建议把float newx = (this.Width )/ X;写成 float newx = (float)(this.Width) / (float)X;否则在手工拉放时比例始终是1另外,如果手工拉申方法来缩小窗口,设置字体时会有异常. 如何实现控件location的变化呢? public float X, Y,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]) * 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 FrmMain_Load(object sender, EventArgs e){ this.Resize += new EventHandler(FrmMain_Resize); X = this.Width; Y = this.Height; y = this.statusStrip1.Height; setTag(this);} private void FrmMain_Resize(object sender, EventArgs e) { // throw new Exception("The method or operation is not implemented."); float newx = (this.Width) / X; // float newy = (this.Height - this.statusStrip1.Height) / (Y - y); float newy = this.Height / Y; setControls(newx, newy, this); this.Text = this.Width.ToString() + " " + this.Height.ToString(); } 关于dataset很稀奇古怪的事 这个dr[1]到底是什么? C#用户登录验证中的字符串比较 从dataGridView中取的数据怎么转类型呀 初学者:为什么我的SendMessage有这样的错误? 在线等了,如何给 textbox (Password 型) 赋值阿, 就是 赋值后显示为 ***** 这样的 如何实现字符类型向枚举类型转换? 我的WEB服务器不能显示WEB组件,是什么原因? 100分讨论概率问题 大哥~~~帮帮忙吧!!在线等!!! 关于bmp、png、jpg转换成ico格式 100分送上 如何检查xml是否格式良好?
-----------不懂,panel还有这个问题吗?
但是我设置panel的Anchor,显示的位置也不对啊 ,还有如果picturebox也这么设置,就更不对了啊
翻譯不一定很准,你稍微微調下。經測試可用。
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 );
}
} } void Form1_Resize(object sender, EventArgs e)
{
// throw new Exception("The method or operation is not implemented.");
float newx = (this.Width )/ X;
// float newy = (this.Height - this.statusStrip1.Height) / (Y - y);
float newy = this.Height / Y;
setControls(newx, newy, this);
this.Text = this.Width.ToString() +" "+ this.Height.ToString(); }
翻译的一篇BLOG,但是注意statusStrip1之类的,自己修改把.
form_load中添加
this.Resize += new EventHandler(Form1_Resize); X = this.Width;
Y = this.Height;
y = this.statusStrip1.Height;
setTag (this);
public float Y;
public float y;
Anchor不会缩放字体,而14楼的会缩放字体,不错...
写成 float newx = (float)(this.Width) / (float)X;
否则在手工拉放时比例始终是1
另外,如果手工拉申方法来缩小窗口,设置字体时会有异常.
{
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 FrmMain_Load(object sender, EventArgs e)
{
this.Resize += new EventHandler(FrmMain_Resize);
X = this.Width;
Y = this.Height;
y = this.statusStrip1.Height;
setTag(this);
} private void FrmMain_Resize(object sender, EventArgs e)
{
// throw new Exception("The method or operation is not implemented.");
float newx = (this.Width) / X;
// float newy = (this.Height - this.statusStrip1.Height) / (Y - y);
float newy = this.Height / Y;
setControls(newx, newy, this);
this.Text = this.Width.ToString() + " " + this.Height.ToString(); }