本文介绍了如何将模态表单居中放置在 SplitContainer 的特定面板上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 SplitContainer 控件分成两部分的 GUI.
一部分是导航面板,另一部分是工作区面板.

I have GUI which is split in two pieces using a SplitContainer Control.
One part is a navigation Panel, the other a workspace Panel.

当我打开应用程序时,在启动时会出现一个新表单(使用 ShowDialog()),欢迎用户.我想在工作区面板的中间居中显示它.

When I open the app, on start-up a new Form appears (using ShowDialog()), to welcomes Users. I would like to show it centered in the middle of the workspace Panel.

有人知道如何解决这个问题吗?

Is there anybody who knows how to solve this?

 Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
     frmWelcome.ShowDialog()
 End Sub

推荐答案

假设 Panel2 是您的 WorkSpace Panel,使用它的 PointToScreen() 方法计算frmWelcome的屏幕坐标并将其定位在中间.

Assuming that Panel2 is your WorkSpace Panel, use its PointToScreen() method to calculate the Screen coordinates of frmWelcome and position it in the middle.

请务必在设计器或其构造函数中设置您的 frmWelcome.StartPosition = Manual.

Be sure to set your frmWelcome.StartPosition = Manual, in the Designer or in its Constructor.

在这里,我使用了 Shown 事件,以确保 MainForm 中的预设位置已经设​​置.

Here, I'm using the Shown event, to be sure that the pre-set positions in MainForm are already set.

Private Sub MainForm_Shown(sender As Object, e As EventArgs) Handles Me.Shown
    Dim p As Point = New Point(
        ((SplitContainer1.Panel2.ClientSize.Width) \ 2) - frmWelcome.Width \ 2,
        ((SplitContainer1.Panel2.ClientSize.Height) \ 2) - frmWelcome.Height \ 2)

    frmWelcome.Location = SplitContainer1.Panel2.PointToScreen(p)
    frmWelcome.ShowDialog()
End Sub

这篇关于如何将模态表单居中放置在 SplitContainer 的特定面板上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 04:04