窗口中的控件问题 50分

lwj_pp 2008-05-16 01:53:25
设计的是一个非最大化的窗口,要求最大化后,控件大小随之改变,不用 通过设置控件的Anchor属性的方法(panel设置Anchor属性就不能正常显示),需要各位提供的代码提示,谢谢!
...全文
499 28 打赏 收藏 转发到动态 举报
写回复
用AI写文章
28 条回复
切换为时间正序
请发表友善的回复…
发表回复
Mysaucer 2012-02-27
  • 打赏
  • 举报
回复
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();

}
Mysaucer 2012-02-27
  • 打赏
  • 举报
回复
14 15楼结合,完美!!!
司马先生v 2011-03-04
  • 打赏
  • 举报
回复
如何实现控件location的变化呢?
EricPan2023 2008-05-22
  • 打赏
  • 举报
回复
建议把float newx = (this.Width )/ X;
写成 float newx = (float)(this.Width) / (float)X;
否则在手工拉放时比例始终是1
另外,如果手工拉申方法来缩小窗口,设置字体时会有异常.
EricPan2023 2008-05-22
  • 打赏
  • 举报
回复
为什么手动拉放窗口却不能用呢?
Anchor不会缩放字体,而14楼的会缩放字体,不错...
baihe_591 2008-05-20
  • 打赏
  • 举报
回复
楼主不公平啊。
viena 2008-05-20
  • 打赏
  • 举报
回复
删除倒分回复,关XHW
rockyvan 2008-05-17
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 lwj_pp 的回复:]
4楼结合14楼就能实现,效果非常好,保留
[/Quote]
呵呵!可以結帖了。
lwj_pp 2008-05-16
  • 打赏
  • 举报
回复
4楼结合14楼就能实现,效果非常好,保留
lwj_pp 2008-05-16
  • 打赏
  • 举报
回复
实现了,永久保留
yagebu1983 2008-05-16
  • 打赏
  • 举报
回复
把控件的左右上下距离父窗体的大小设定成固定值就可以!!
baihe_591 2008-05-16
  • 打赏
  • 举报
回复
其实3楼说的很有道理,
baihe_591 2008-05-16
  • 打赏
  • 举报
回复
public float X;
public float Y;
public float y;
baihe_591 2008-05-16
  • 打赏
  • 举报
回复

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);
lwj_pp 2008-05-16
  • 打赏
  • 举报
回复
我调下,有什么好的方法,指教一下,谢谢了
rockyvan 2008-05-16
  • 打赏
  • 举报
回复
4樓結合這個網站:http://labs.developerfusion.co.uk/convert/csharp-to-vb.aspx
翻譯不一定很准,你稍微微調下。經測試可用。
lwj_pp 2008-05-16
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 BIGBIRDINWOODS 的回复:]
Resize()事件里面调整。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
[/Quote]


谢谢李连杰,我知道在那里写,我需要点代码提示,我自己写不出来
BIGBIRDINWOODS 2008-05-16
  • 打赏
  • 举报
回复
Resize()事件里面调整。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
wwlprince 2008-05-16
  • 打赏
  • 举报
回复
同意7楼
Fanks 2008-05-16
  • 打赏
  • 举报
回复
可以通过使用tablelayerpanel和控件的dock属性来实现,不过比Anchor要费很多事啊。
加载更多回复(7)

110,577

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧