2011-06-14 88 views
6

我正在使用Winform應用程序並希望打開父窗體中心的模態窗體。在WinForm應用程序有:在MDI應用程序的父窗體中心打開模態窗口

  1. MDI窗體(開放的啓動形式,並作爲所有容器)
  2. 對MDI窗體的菜單項之一的點擊 - 打開一個MDI子窗體
  3. 上單擊步驟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但它對我的問題沒有幫助。

任何人都可以有任何建議或解決方案來解決問題。

感謝

回答

1

試試這個:

TestModalForm.showdialog(this.MdiParent); 
+1

if(this.MdiParent!= null)... – 2011-06-14 08:56:38

+0

Hello Davide,TestModalForm.showdialog(this.MdiParent); 它不工作。另外this.MdiParent給null,因此它顯示在MDI的中心 - 這不是必需的。我需要在第二個MDI子中心打開第三個模態窗口。它的第三個問題開始。 – Shah 2011-06-14 09:52:24

0

你的方法似乎過於複雜。爲什麼不這樣做呢?

// All code goes in your MDI Child form 

// Create the modal form 
TestModalForm obj = new TestModalForm(); 

// Find center point of MDI Parent form 
Point centerPoint = new Point(this.MdiParent.Top + this.MdiParent.Height/2, 
           this.MdiParent.Left + this.MdiParent.Width/2); 
// Set the location and show the modal form 
obj.StartPosition = FormStartPosition.Manual 
obj.Location = centerPoint; 
obj.ShowDialog(this); 
0

我認爲你應該做這個

//for modal form set its strat position to centerparent like this 

**this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;** 

// this will open the modalform in center of parent form 
+0

它使對話框顯示在MDI Parent的中心,而OP需要在MDI Child窗體的中心顯示。 – 2016-07-27 23:04:49

0

在MDI形成center to parent預期看到文檔MSDN,它總是建議使用center to screen不工作,但我用一個變通對此,畢竟是關於位置定位,所以我創建了一個覆蓋方法center to parent,並在子表單load event上使用它,以便每次孩子被加載時它都以父母爲中心:

private void CenterToParentOverride() 
    { 
     this.Location = new Point(
      this.MdiParent.ClientSize.Width/2 - this.Width/2, 
      this.MdiParent.ClientSize.Height/2 - this.Height/2); 
    } 
+0

OP要顯示MDI中心中的對話框不在MDI父窗體的中心。 – 2016-07-27 23:03:28

0

爲了展示形式在其父的中心使用ShowDialog當父是MDI子,你可以用下面的代碼

的代碼應該從一個按鈕在MDI子形式和節目運行另一種形式爲模態對話框在MDI子形式的中心:

var dialog = new Form(); //The form which you want to show as dialog 
//this.Parent point to the MdiClient control which is the container of this 
var p = this.Parent.PointToScreen(this.Location); 
p.Offset((this.Width - dialog.Width)/2 , (this.Height - dialog.Height)/2); 
dialog.StartPosition = FormStartPosition.Manual; 
dialog.DesktopLocation = p; 
dialog.ShowDialog(); 

在上面的代碼中,由於電流的形式是MDI子,然後this.Parent點到t他是MdiClient控制哪個是MDI子窗體的容器,所以我們可以用它的PointToScreen方法通過MDI子窗體的位置(這個)來獲取MDI子窗體的位置。然後,如果您使用MDI子對話框的寬度和高度之差的一半來抵消該位置,並將對話框的StartPosition設置爲FormStartPosition.Manual,並使用ShowDialog來顯示它。

+0

我知道這個問題很舊,但目前所有其他答案都是錯誤的。 10k視圖的問題需要一個正確的答案。 – 2016-07-27 23:05:34

相關問題