1. 程式人生 > >VB.Net 設定子視窗在父視窗居中顯示

VB.Net 設定子視窗在父視窗居中顯示

    Private Function SetLocation(ByVal insForm As Form) As Point

        'Screen.AllScreens[0]是獲取當前顯示器裝置視窗的第一個
        Dim insScreen As Screen = Screen.AllScreens(0)
        If (Not Me.OwnerForm Is Nothing) Then
            '獲取控制元件父視窗的螢幕
            insScreen = Screen.FromControl(Me)
        End If
        '當前螢幕的寬度 - 子頁面的寬度
        Dim width As Integer = (insScreen.WorkingArea.Width - insForm.Width)
        '當前螢幕的高度 - 子頁面的高度
        Dim height As Integer = (insScreen.WorkingArea.Height - insForm.Height)
        '居中
        Dim x As Integer = (insScreen.WorkingArea.X + CInt(Math.Round(CDbl((CDbl(width) / 2)))))
        Dim y As Integer = (insScreen.WorkingArea.Y + CInt(Math.Round(CDbl((CDbl(height) / 2)))))
        '啟動模式
        insForm.StartPosition = FormStartPosition.Manual
        '返回
        Return New Point(x, y)

    End Function