在MDI应用程序的父窗体的中心打开模态窗口。

| 我正在Winform应用程序上工作,想在父窗体的中心打开模式窗体。在Winform应用程序中有: MDI表单(作为启动表单打开,并充当所有容器) 单击MDI表单的菜单项之一时-打开MDI子表单 在第2步中打开的MDI Child的按钮之一上单击时,将打开-模态表单-我们需要在MDI Child表单的中心打开(在第2步中打开) 因此,对于在第一中心打开模态形式,我做的明显解决方案是
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子表单的中心打开模式表单 还看了这个问题,但对我的问题没有帮助。 任何人都可以有任何建议或解决方案来解决问题。 谢谢     
已邀请:
        尝试这个:
TestModalForm.showdialog(this.MdiParent);
    
        您的方法似乎过于复杂。为什么不这样做呢?
// 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);
    
        我认为你应该这样做
//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
    
        在MDI格式中,
center to parent
不能正常工作,请参阅文档MSDN,始终建议使用
center to screen
,但是我为此使用了一种变通方法,毕竟它与位置定位有关,因此我为
center to parent
并在子窗体form8ѭ上使用它,以便每次加载孩子时都将其居中于父对象:
private void CenterToParentOverride()
   {
      this.Location = new Point(
          this.MdiParent.ClientSize.Width / 2 - this.Width / 2,
          this.MdiParent.ClientSize.Height / 2 - this.Height / 2);
   }
    
        要在父级为MDI子级时在其父级的中心使用ѭ10来显示表单,可以使用以下代码 该代码应该从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
指向the13ѭ控件,该控件是MDI子窗体的容器,因此我们可以使用其
PointToScreen
方法传递MDI子窗体(此)的位置来获取MDI子窗体的屏幕位置。 MDI的孩子。然后,如果使用MDI子项与对话框宽度和高度之差的一半来偏移该位置,并将对话框的“ 15”设置为“ 16”,然后使用“ 10”显示它。     

要回复问题请先登录注册