我正在使用Winform應用程序並希望打開父窗體中心的模態窗體。在WinForm應用程序有:在MDI應用程序的父窗體中心打開模態窗口
- MDI窗體(開放的啓動形式,並作爲所有容器)
- 對MDI窗體的菜單項之一的點擊 - 打開一個MDI子窗體
- 上單擊步驟2中打開的MDI子按鈕中的一個 - 打開一個模式窗體 - 這是我們需要打開MDI子窗體的中心
(在步驟2中打開),所以在中心1日開放模式窗體我所做的明顯解決方案是
TestModalForm obj = new TestModalForm()
obj.StartPosition = FormStartPosition.CenterParent;
obj.showdialog(this);
但上面的解決方案沒有工作,因爲模式形式始終將MDI窗體視爲其父項。對於第二個解決方案,使我鍛鍊:在我的模式窗口的形式加載的方法寫在下面中心其定位:
private void MakeWinInCenter()
{
if (this.Owner != null)
{
Form objParent = null;
int TopbarHeight = 0;
if (this.Owner.IsMdiContainer && this.Owner.ActiveMdiChild != null)
{
objParent = this.Owner.ActiveMdiChild;
TopbarHeight = GetTopbarHeight(this.Owner);
}
else
objParent = this.Owner;
Point p = new Point((objParent.Width - this.Width)/2, (objParent.Height - this.Height)/2);
p.X += objParent.Location.X;
p.Y += TopbarHeight + objParent.Location.Y;
this.Location = p;
}
else
{
//If owner is Null then, we have reference of MDIForm in Startup Class - use that ref and opens win in center of MDI
if (Startup.MDIObj != null)
{
this.Left = Convert.ToInt32((Startup.MDIObj.Width - this.Width)/2);
this.Top = Convert.ToInt32((Startup.MDIObj.Height - this.Height)/2);
}
}
}
private int GetTopbarHeight(Form MDIForm)
{
int TopbarHeight = 0;
MdiClient objMDIClient = null;
foreach (Control ctl in MDIForm.Controls)
{
if (ctl is MdiClient)
{
objMDIClient = ctl as MdiClient;
break;
}
}
if (objMDIClient != null)
{
TopbarHeight = MDIForm.Height - objMDIClient.Size.Height;
}
return TopbarHeight;
}
上述方案時,MDI形式最大化的形式打開運行完美。但是,當我們通過調整MDI表格(即不是最大化形式)或將MDI表格移動到其他屏幕來進行檢查時 - 在多個屏幕的情況下,上述解決方案不起作用,並且不打開MDI中心中的模式形式子表格
也看過this Question但它對我的問題沒有幫助。
任何人都可以有任何建議或解決方案來解決問題。
感謝
if(this.MdiParent!= null)... – 2011-06-14 08:56:38
Hello Davide,TestModalForm.showdialog(this.MdiParent); 它不工作。另外this.MdiParent給null,因此它顯示在MDI的中心 - 這不是必需的。我需要在第二個MDI子中心打開第三個模態窗口。它的第三個問題開始。 – Shah 2011-06-14 09:52:24