2011-06-14 2 views
6

Winform 응용 프로그램에서 작업 중이며 상위 양식 가운데에 모달 양식을 열고 싶습니다. 의 WinForm 응용 프로그램에서이 :MDI 응용 프로그램의 부모 양식 가운데에있는 모달 창 열기

  1. MDI 폼
  2. MDI 폼의 메뉴 항목 중 하나를 클릭에
  3. 는 (모든 컨테이너로 시작 폼 행동 오픈) - 한 MDI 자식 폼
  4. 에 열립니다 MDI의 버튼 중 하나를 클릭합니다. 2 단계에서 열린 하위 버튼을 클릭합니다. - MDI 가운데에 열어야하는 모달 형식을 엽니 다. 하위 양식 (2 단계에서 열림)

첫 번째 모달 양식 내가 해낸 확실한 해결책은

입니다.
TestModalForm obj = new TestModalForm() 
obj.StartPosition = FormStartPosition.CenterParent; 
obj.showdialog(this); 

위의 해결 방법은 모달 형식이 항상 MDI 폼을 부모로 간주하기 때문에 작동하지 않았습니다. 2 솔루션에 대한 그래서 운동 : 내가 모달 창 폼로드의 방법을 썼다는 점에서 아래와 같이 중앙에 위치합니다 : 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 가운데에 모달 형식이 열리지 않습니다. 자식 양식

또한 this Question에서 보았지만 내 문제에는 도움이되지 않습니다.

누구든지 문제를 해결할 수있는 제안이나 해결책이 있습니까?

감사

답변

1

이 시도 :

TestModalForm.showdialog(this.MdiParent); 
+1

if (this.MdiParent! = null) ... –

+0

안녕하세요 Davide, TestModalForm.showdialog (this.MdiParent); 작동하지 않습니다. 또한 this.MdiParent는 MDI의 가운데에 표시되도록 null을 제공합니다. 필요하지는 않습니다. 제 2 차 MDI 아동 센터에서 3 차 모달 창을 열어야합니다. 그 질문의 시작에서 세 번째 지점. – Shah

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

난 당신이 MDI에서이

//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 부모의 가운데에 대화 상자가 표시되는 반면 OP는 MDI 자식 폼의 가운데에 표시되어야합니다. –

0

이 문서 MSDN, 항상 center to screen를 사용하는 것이 좋습니다 참조 예상대로 작동하지 않습니다 center to parent을 형성해야한다 생각하지만, 나는 해결 방법 사용 이것은 결국 위치 지정에 관한 것이므로 center to parent에 대한 재정의 메소드를 작성하고 하위 양식 load event에 사용하므로 하위가로드 될 때마다 상위에 중앙에 배치됩니다.

+0

OP는 MDI 학부모 양식의 중심에 있지 않은 MDI 아동의 중심에 대화 상자를 표시하려고합니다. –

0

private void CenterToParentOverride() 
    { 
     this.Location = new Point(
      this.MdiParent.ClientSize.Width/2 - this.Width/2, 
      this.MdiParent.ClientSize.Height/2 - this.Height/2); 
    } 
는 부모가 MDI 자식 때, 당신은 코드가 MDI 자식 형태와 표시의 버튼에서 실행하도록되어

코드

를 사용할 수 있습니다 부모의 중앙에 ShowDialog를 사용하여 양식을 표시하려면 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 포인트입니다 그는 MdiClient 컨트롤은 MDI 자식 폼의 컨테이너이므로 우리는 PointToScreen 메서드를 사용하여 MDI 자식 (this)의 위치를 ​​전달하여 MDI 자식의 화면 위치를 가져올 수 있습니다. 그런 다음 MDI 하위와 대화 상자 너비와 높이의 차이의 절반을 사용하여 해당 위치를 오프셋하고 대화 상자의 StartPositionFormStartPosition.Manual으로 설정하고 ShowDialog을 사용하여 표시합니다.

+0

나는 그 질문이 오래되었다는 것을 알고 있지만, 현재 다른 모든 대답은 틀렸다. 1 만 건의 조회가있는 질문에 정답이 필요합니다. –

관련 문제