我正在使用Winform应用程序,并希望在父窗体的中心打开模态窗体.在Winform应用程序中有:
因此,对于在中心打开模态形式,我所做的第一个明显的解决方案是
TestModalForm obj = new TestModalForm()
obj.StartPosition = FormStartPosition.CenterParent;
obj.showdialog(this);
Run Code Online (Sandbox Code Playgroud)
但上述解决方案不起作用,因为模态形式始终将MDI表格视为其父级.所以我为第二个解决方案做准备:我在Form Load of Modal窗口中编写方法将它放在中心,如下所示:
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;
}
Run Code Online (Sandbox Code Playgroud)
当MDI表格以最大化形式打开时,上述解决方案非常完美.但是,当我们通过调整MDI表单(即不是最大化形式)或将MDI表单移动到其他屏幕进行检查时 - 如果是多个屏幕,则上述解决方案无效并且不会在MDI子窗体的中心打开模式窗体
还看了这个问题,但它对我的问题没有帮助.
任何人都可以有任何建议或解决方案来解决问题.
谢谢
| 归档时间: |
|
| 查看次数: |
12095 次 |
| 最近记录: |