Lisong's Blog


十二月 25th, 2008

禁止子窗体超出MdiParent父窗体范围

Learning, by 木公.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
MdiChildForm mdiChildForm=null;//这个是子窗体
TmpChildForm tmpChildForm=null;//这个是临时窗体对象
 
//自定义子窗体关闭事件
private void tmpChildForm_FormClosed(object sender, FormClosedEventArgs e)
{
this.tmpChildForm = null;
}
 
//自定义子窗体Move事件
private void tmpChildForm_Move(object sender, EventArgs e)
{
if (tmpChildForm.Left < 0)
{
tmpChildForm.Left = 0;
}
if (tmpChildForm.Left > this.Width - tmpChildForm.Width - 15)
{
tmpChildForm.Left = this.Width - tmpChildForm.Width - 15;
}
if (tmpChildForm.Top < 0)
{
tmpChildForm.Top = 0;
}
if (tmpChildForm.Top > this.Height - tmpChildForm.Height - 65)
{
tmpChildForm.Top = this.Height - tmpChildForm.Height - 65;
}
}
 
//父窗体单击事件
private void openMdiChileForm_Click(object sender, EventArgs e)
{
if (mdiChildForm == null)
{
mdiChildForm = new MdiChildForm();
mdiChildForm.MdiParent = this;
mdiChildForm.Show();
tmpChildForm = mdiChildForm;
mdiChildForm.Move += new EventHandler(tmpChildForm_Move);
mdiChildForm.FormClosed += new FormClosedEventHandler(tmpChildForm_FormClosed);
}
else
{
mdiChildForm.Activate();
}
}

将此代码添加到父窗体cs中即可,代码中的两个数字根据实际情况改变。
如果有多个子窗体,只需要按照openMdiChileForm_Click事件调用即可。

隐藏父窗体滚动条的方法详见:http://imwls.com/article.asp?id=44

Back Top

回复自“禁止子窗体超出MdiParent父窗体范围”

评论 (0) 引用 (0) 发表评论 引用地址
  1. 没有任何评论。
  1. 没有任何引用。

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*


无觅相关文章插件,快速提升流量