2015-01-02 1 views
0

제 코드는 작성하지 말고 경로를 묻는 것입니다.부모 양식을 종료 할 때 양식을 중지하십시오.

나는 열린 폼이 부모 폼의 경계를 떠나지 못하도록하는 방법을 찾고 있습니다.

두 양식을 모두 열어 두지 만 다른 프로그램의 경계를 벗어나는 프로그램을 통해 열리는 다른 양식은 허용되지 않습니다. 가장 좋은 예는 과 같습니다. 무슨 뜻인지의

사진 :

enter image description here

감사합니다, 추가 질문은 문의하시기 바랍니다! Austin

+0

'this.Hide() : 아래

예제 코드는 MSDN 포럼에 아주 오래된 질문에서,하지만 여전히 매력 : 소스처럼 작동? – Prix

+0

오스틴, 귀하의 질문에 명확하지 않습니다. "열린 양식이 상위 양식의 경계를 벗어나지 않도록 방지"한다는 것은 무엇을 의미합니까? –

+0

당신은 부모 - 자식 관계를 만들려고합니까? - http://stackoverflow.com/questions/2217716/c-sharp-set-the-parent-of-a-form –

답변

1

MDI 솔루션은 시작 지점이지만 하위 MDI 양식은 여전히 ​​상위 MDI 폼의 보이는 창의 경계 밖으로 이동할 수 있습니다. 이 문제를 해결하려면 이벤트 핸들러를 자식 MDI 폼에 추가해야 각 자식 창이 이동 한 후에 부모 MDI 폼에 남아있게됩니다. `; https://social.msdn.microsoft.com/Forums/windows/en-US/46e35e80-7bfa-447a-9655-965134124f70/prevent-child-form-from-leaving-parent-form-bounds?forum=winforms

protected override void OnMove(EventArgs e) 
{ 
    // 
    // Get the MDI Client window reference 
    // 
    MdiClient mdiClient = null; 
    foreach(Control ctl in MdiParent.Controls) 
    { 
     mdiClient = ctl as MdiClient; 
     if(mdiClient != null) 
      break; 
    } 
    // 
    // Don't allow moving form outside of MDI client bounds 
    // 
    if(Left < mdiClient.ClientRectangle.Left) 
     Left = mdiClient.ClientRectangle.Left; 
    if(Top < mdiClient.ClientRectangle.Top) 
     Top = mdiClient.ClientRectangle.Top; 
    if(Top + Height > mdiClient.ClientRectangle.Height) 
     Top = mdiClient.ClientRectangle.Height - Height; 
    if(Left + Width > mdiClient.ClientRectangle.Width) 
     Left = mdiClient.ClientRectangle.Width - Width; 
    base.OnMove(e); 
} 
관련 문제