Program Tip

C #에서 부모 양식의 중앙에 자식 양식 표시

programtip 2020. 10. 28. 20:35
반응형

C #에서 부모 양식의 중앙에 자식 양식 표시


새 양식을 만들고 다음과 같이 상위 양식에서 호출합니다.

loginForm = new SubLogin();   
loginForm.Show();

부모의 중앙에 자식 양식을 표시해야합니다. 따라서 자식 양식로드에서 다음을 수행합니다 .`

Point p = new Point(this.ParentForm.Width / 2 - this.Width / 2, this.ParentForm.Height / 2 - this.Height / 2);
this.Location = p;

그러나 이것은 부모 양식이 null이므로 오류가 발생합니다. Parent 속성도 설정하려고 시도했지만 도움이되지 않았습니다. 이것에 대한 어떤 입력?


시험:

loginForm.StartPosition = FormStartPosition.CenterParent;
loginForm.ShowDialog(this);

물론 아이의 지금 다음 바람직하지 않은 경우 바로 교체, 부모 윈도우의 차단 양식 (대화)입니다 위해 ShowDialog함께 Show..

loginForm.Show(this);

그래도 StartPosition을 지정해야합니다.


"부모"와 "소유자"사이에 혼동이있는 것 같습니다. 양식을 MDI 양식으로 여는 경우 (즉, 다른 양식에 임베드 된 경우)이 주변 양식이 상위 양식입니다. 값이 FormStartPosition.CenterParent 인 양식 속성 StartPosition이이를 참조합니다. Show 메서드에 전달할 수있는 매개 변수는 부모가 아니라 소유자입니다! 이것이 frm.StartPosition = FormStartPosition.CenterParent가 예상대로 작동하지 않는 이유입니다.

폼에 배치 된 다음 코드는 StartPosition이 Manual로 설정되어있는 경우 오프셋이있는 소유자와 관련하여 중앙에 배치됩니다. 작은 오프셋은 타일 방식으로 양식을 엽니 다. 소유자와 소유 한 양식의 크기가 같거나 소유 한 여러 양식을 여는 경우 이점이 있습니다.

protected override void OnShown(EventArgs e)
{
    base.OnShown(e);
    if (Owner != null && StartPosition == FormStartPosition.Manual) {
        int offset = Owner.OwnedForms.Length * 38;  // approx. 10mm
        Point p = new Point(Owner.Left + Owner.Width / 2 - Width / 2 + offset, Owner.Top + Owner.Height / 2 - Height / 2 + offset);
        this.Location = p;
    }
}

코드가 부모 양식 내에서 실행되고 있다고 가정하면 다음과 같은 것이 아마도 당신이 찾고있는 것일 것입니다.

loginForm = new SubLogin();
loginForm.StartPosition = FormStartPosition.CenterParent
loginForm.Show(this);

기록을 Form.CenterToParent()위해 어떤 이유로 든 생성 후 중앙에 배치해야하는 경우 기능도 있습니다.


를 사용하지 않으면 부모 설정이 작동하지 않습니다 form.ShowDialog();.

내가 사용할 때까지 사용 form.Show();하거나 form.Show(this);아무것도 작동하지 않으면 this.CenterToParent();. 양식 Load 메서드에 넣었습니다 . 모두 좋은.

부모 중심의 시작 위치가 설정되었으며 차단 표시 대화 상자를 사용할 때 작동합니다.


양식 내에서 MDIForm양식을 시작할 때 .CenterScreen대신 을 사용해야 합니다 .CenterParent.

FrmLogin f = new FrmLogin();
f.MdiParent = this;
f.StartPosition = FormStartPosition.CenterScreen;
f.Show();

다음이 필요합니다.

나를 this.parent로 대체하지만 해당 양식을 표시하기 전에 parent를 설정해야합니다.

  Private Sub ÜberToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ÜberToolStripMenuItem.Click

        'About.StartPosition = FormStartPosition.Manual ' !!!!!
        About.Location = New Point(Me.Location.X + Me.Width / 2 - About.Width / 2, Me.Location.Y + Me.Height / 2 - About.Height / 2)
        About.Show()
    End Sub

childform = new Child();
childform.Show(this);

이벤트 자식 양식로드

this.CenterToParent();

모든 경우에 작동하며 Form1을 기본 양식으로 바꿉니다.

Popup popup = new Popup();
popup.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
popup.Location = new System.Drawing.Point((Form1.ActiveForm.Location.X + Form1.ActiveForm.Width / 2) - (popup.Width / 2),(Form1.ActiveForm.Location.Y + Form1.ActiveForm.Height / 2) - (popup.Height / 2));
popup.Show(Form1.ActiveForm);

SubLogin 양식에서 부모 양식에서 설정할 수 있도록 SetLocation 메서드를 노출합니다.

public class SubLogin : Form
{
   public void SetLocation(Point p)
   {
      this.Location = p;
   }
} 

그런 다음 기본 양식에서 :

loginForm = new SubLogin();   
Point p = //do math to get point
loginForm.SetLocation(p);
loginForm.Show();

자신의 위치를 ​​계산하려면 먼저 다음으로 설정 StartPosition하십시오 FormStartPosition.Manual.

Form Child = new Form();
Child.StartPosition = FormStartPosition.Manual;
Child.Location = new Point(Location.X + (Width - Child.Width) / 2, Location.Y + (Height - Child.Height) / 2);
Child.Show(this);

이것이 Location.X와 마찬가지로 주 / 부모 양식 인 곳입니다.

의 기본값 StartPositionFormStartPosition.CenterParent이므로 표시 후 자녀의 위치를 ​​변경합니다.


Windows Form을 만든 다음 옵션을 넣으십시오. CenterParent 다음 코드를 사용하십시오.

yourChildFormName x = new yourChildFormName();
x.ShowDialog();

폼의 모든 새 인스턴스가 부모의 중앙에 오도록 자식 폼의 생성자에서 StartPosition을 설정할 수 있습니다.

public MyForm()
{
    InitializeComponent();

    this.StartPosition = FormStartPosition.CenterParent;
}

Of course, you could also set the StartPosition property in the Designer properties for your child form. When you want to display the child form as a modal dialog, just set the window owner in the parameter for the ShowDialog method:

private void buttonShowMyForm_Click(object sender, EventArgs e)
{
    MyForm form = new MyForm();
    form.ShowDialog(this);
}

If any windows form(child form) is been opened from a new thread of Main window(parent form) then its not possible to hold the sub window to the center of main window hence we need to fix the position of the sub window manually by means of X and Y co-ordinates.

In the properties of Subwindow change the "StartPosition" to be "Manual"

code in main window

private void SomeFunction()
{
    Thread m_Thread = new Thread(LoadingUIForm);
    m_Thread.Start();
    OtherParallelFunction();
    m_Thread.Abort();
}

private void LoadingUIForm()
{
    m_LoadingWindow = new LoadingForm(this);
    m_LoadingWindow.ShowDialog();
}

code in subwindow for defining its own position by means of parent current position as well as size

public LoadingForm(Control m_Parent)
{
   InitializeComponent();
   this.Location = new Point( m_Parent.Location.X+(m_Parent.Size.Width/2)-(this.Size.Width/2),
                              m_Parent.Location.Y+(m_Parent.Size.Height/2)-(this.Size.Height/2)
                            );
}

Here the co-ordinates of center of parent is calculated as well as the subwindow is kept exactly at the center of the parent by calculating its own center by (this.height/2) and (this.width/2) this function can be further taken for parent relocated events also.


The parent probably isn't yet set when you are trying to access it.

Try this:

loginForm = new SubLogin();
loginForm.Show(this);
loginForm.CenterToParent()

If you have to center your childForm, from childForm then the code will be something like this. This code is in the childForm.cs

this.Show(parent as Form);    // I received the parent object as Object type
this.CenterToParent();

    protected override void OnLoad(EventArgs e) {
        base.OnLoad(e);

        CenterToParent();
    }

Why not use this?

LoginForm.WindowStartupLocation = Windows.WindowStartupLocation.CenterOwner 

(vb.net)

참고URL : https://stackoverflow.com/questions/944897/show-a-child-form-in-the-centre-of-parent-form-in-c-sharp

반응형