Option Explicit Private Type RECT Left As Long Top As Long Right As Long Bottom As Long End TypePrivate Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Private Declare Function MoveWindow Lib "user32" (ByVal hwnd As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long Private l As LongPrivate Sub Form_Load() l = FindWindow(vbNullString, "打开") If l <> 0 Then Dim h As Long Dim w As Long Dim r As RECT
GetWindowRect l, r h = r.Bottom - r.Top w = r.Right - r.Left MoveWindow l, (Screen.Width / Screen.TwipsPerPixelX - w) / 2, (Screen.Height / Screen.TwipsPerPixelY - h) / 2, w, h, 0 End If End Sub
我下载运行了一下,可以符合你的要求。
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End TypePrivate Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function MoveWindow Lib "user32" (ByVal hwnd As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private l As LongPrivate Sub Form_Load()
l = FindWindow(vbNullString, "打开")
If l <> 0 Then
Dim h As Long
Dim w As Long
Dim r As RECT
GetWindowRect l, r
h = r.Bottom - r.Top
w = r.Right - r.Left
MoveWindow l, (Screen.Width / Screen.TwipsPerPixelX - w) / 2, (Screen.Height / Screen.TwipsPerPixelY - h) / 2, w, h, 0
End If
End Sub
先看看那个文件对话框的大小,然后建一个大小相同的空白窗体,把窗体的StartPosition属性设置为2(屏幕中心),在窗体的Load事件中显示文件对话框,你会发现文件对话框刚好与窗体重合,由于窗体是位于屏幕中央的,那么这个文件对话框也位于屏幕中央了,最后为了防止用户拖动对话框而看到窗体,不要Show窗体,只要Load窗体就可以了。