有一个固定大小的窗体如何让它无论在任何分辨率下都左右相对屏幕边界,上下相对上屏幕边界和开始菜单任务栏边界居中?

解决方案 »

  1.   

    StartUpPosition属性设为2不可以吗?
      

  2.   

    Option Explicit
    Private Declare Function SHAppBarMessage Lib "shell32.dll" _
        (ByVal dwMessage As Long, pData As APPBARDATA) As LongPrivate Const ABM_GETTASKBARPOS = &H5
    Private Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
    End Type
    Private Type APPBARDATA
        cbSize As Long
        hWnd As Long
        uCallbackMessage As Long
        uEdge As Long
        rc As RECT
        lParam As Long
    End TypePrivate Sub Form_Load()
        Dim MyData As APPBARDATA    MyData.cbSize = Len(MyData)
        Call SHAppBarMessage(ABM_GETTASKBARPOS, MyData)
        '任務欄有三個邊都在屏幕外2個像素
        With MyData.rc
            If .Top > 0 Then    '任務欄在最下面
                Me.Move (Screen.Width - Width) \ 2, (.Top * Screen.TwipsPerPixelY - Height) \ 2
            ElseIf .Bottom < Screen.Height \ Screen.TwipsPerPixelY Then     '任務欄在頂上
                Me.Move (Screen.Width - Width) \ 2, (Screen.Height - Height + .Bottom * Screen.TwipsPerPixelY) \ 2
            ElseIf .Left > 0 Then   '在最右邊
                Me.Move (.Left * Screen.TwipsPerPixelX - Width) \ 2, (Screen.Height - Height) \ 2
            Else
                Me.Move (Screen.Width - Width + .Right * Screen.TwipsPerPixelX) \ 2, (Screen.Height - Height) \ 2
            End If
        End With
    End Sub