2017-06-15 138 views
0

我需要在活动Excel窗体的中心显示一个客户窗体。 FormStartPosition.CenterParent不起作用。 我尝试下面的代码如何在VSTO AddIn的Excel窗口中心显示窗体窗体

Private Sub myForm_Load(sender As Object, e As EventArgs) Handles Me.Load 

    app = Globals.ThisAddIn.Application 

    Me.Left = app.Left + app.Width/2 - Me.Width/2 
    Me.Top = app.Top + app.Height/2 - Me.Height/2 

End Sub 

可悲的是,上面的代码不工作如我所料。该表单不位于Excel应用程序的中心。我怀疑app.Width和Me.Width使用不同的单位。请帮助我,如果你知道如何解决这个问题。

回答

0

WinForm的大小是像素和ExcelApp在所以你需要app.ActiveWindow.PointsToScreenPixelsX

Private Sub myForm_Load(sender As Object, e As EventArgs) Handles Me.Load 

    app = Globals.ThisAddIn.Application 

    Me.Left = app.ActiveWindow.PointsToScreenPixelsX(app.Left) + _ 
     app.ActiveWindow.PointsToScreenPixelsX(app.Width)/2 - _ 
     Me.Width/2 
    Me.Top = app.ActiveWindow.PointsToScreenPixelsX(app.Top) + _ 
     app.ActiveWindow.PointsToScreenPixelsX(app.Height)/2 - _ 
     Me.Height/2 

End Sub 

我没有测试,但它应该为你工作。

另一种方法是使用ShowDialog显示表单中并设置StartPosition属性为CenterParent

相关问题