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
回复自“禁止子窗体超出MdiParent父窗体范围”