Option Compare Database
Option Explicit
'判断窗体是否最大化
Private Declare Function IsZoomed Lib "user32" (ByVal hwnd As Long) As Long
'显示窗体
Public Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
'判断指定窗口的父窗口
Public Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
'返回指定窗口客户区矩形的大小
Public Declare Function GetClientRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
'指定一个窗口的新父
Public Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
'改变指定窗口的位置和大小
Public 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
'这个函数能为窗口指定一个新位置和状态。它也可改变窗口在内部窗口列表中的位置。
Public Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Type RECT
X1 As Long
Y1 As Long
x2 As Long
y2 As Long
End Type
Public Const SW_SHOWNOACTIVATE = 4 '设置打开窗体是否获得焦点
Public Const SW_MAXIMIZE = 3
Public Const SW_SHOWNORMAL = 1
Sub MaximizeRestoredForm(F As Form)
Dim MDIRect As RECT
' If the form is maximized, restore it.
If IsZoomed(F.hwnd) <> 0 Then
ShowWindow F.hwnd, SW_SHOWNORMAL
End If
' Get the screen coordinates and window size of the
' MDIClient area.
'This is the line which is different
GetClientRect GetParent(F.hwnd), MDIRect
SetParent F.hwnd, GetParent(F.hwnd)
' Move the form to the upper left corner of the MDIClient
' window (0,0) and size it to the same size as the
' MDIClient window.
MoveWindow F.hwnd, 0, 0, MDIRect.x2 - MDIRect.X1, MDIRect.y2 - MDIRect.Y1, True
End Sub