Option Explicit
#If VBA7 Then
Public Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName$, ByVal lpWindowName$) As LongPtr
Public Declare PtrSafe Function SetParent Lib "user32" (ByVal hWndChild As LongPtr, ByVal hWndNewParent As LongPtr) As Long
#If Win64 Then
Public Declare PtrSafe Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As LongPtr, ByVal hWnd2 As LongPtr, ByVal lpsz1 As String, ByVal lpsz2 As String) As LongPtr
Public Declare PtrSafe Function SetWindowLong Lib "user32" Alias "SetWindowLongPtrA" (ByVal hWnd As LongPtr, ByVal nIndex As LongPtr, ByVal dwNewLong As LongPtr) As LongPtr
Public Declare PtrSafe Function GetWindowLong Lib "user32" Alias "GetWindowLongPtrA" (ByVal hWnd As LongPtr, ByVal nIndex As LongPtr) As LongPtr
#Else
Public Declare PtrSafe Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As LongPtr, ByVal hWnd2 As LongPtr, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Public Declare PtrSafe Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As LongPtr, ByVal nIndex As LongPtr, ByVal dwNewLong As LongPtr) As Long
Public Declare PtrSafe Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As LongPtr, ByVal nIndex As LongPtr) As Long
#End If
#Else
Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName$, ByVal lpWindowName$) As Long
#End If
Public Const GWL_STYLE = (-16)
Public Const WS_MAXIMIZEBOX = &H10000
Public Const WS_MINIMIZEBOX = &H20000
Public Const WS_THICKFRAME = &H40000
Public Const WS_SIZEBOX = &H40000
Public Const WS_MAXIMIZE = &H1000000
Public Const WS_MINIMIZE = &H20000000
#If VBA7 Then
Public Function FormHandle(Optional ByVal Caption$ = vbNullString) As LongPtr
#Else
Public Function FormHandle(Optional ByVal Caption$ = vbNullString) As Long
#End If
If Val(Application.Version) < 9 Then
FormHandle = FindWindow("ThunderXFrame", Caption) 'XL97
Else
FormHandle = FindWindow("ThunderDFrame", Caption) 'XL2000
End If
End Function
#If VBA7 Then
Public Sub FormInitialize(ByVal Form As Object, _
ByRef hWnd As LongPtr, _
ByRef Style As LongPtr, _
ByRef oWidth%, _
ByRef oHeight%)
#Else
Public Sub FormInitialize(ByVal Form As Object, _
ByRef hWnd As Long, _
ByRef Style As Long, _
ByRef oWidth%, _
ByRef oHeight%)
#End If
hWnd = FormHandle
Style = GetWindowLong(hWnd, GWL_STYLE)
SetWindowLong hWnd, GWL_STYLE, Style Or WS_SIZEBOX Or WS_MINIMIZEBOX Or WS_MAXIMIZEBOX
oWidth = Form.Width: oHeight = Form.Height
End Sub
#If VBA7 Then
Public Sub FormTerminate(ByVal hWnd As LongPtr, ByVal Style As LongPtr)
#Else
Public Sub FormTerminate(ByVal hWnd As LongPtr, ByVal Style As LongPtr)
#End If
SetWindowLong hWnd, GWL_STYLE, Style
End Sub
Public Sub FormResize(ByVal Form As Object, _
Optional ByVal LWidth% = 200, _
Optional ByVal LHeight% = 200, _
Optional ByVal oWidth% = 200, _
Optional ByVal oHeight% = 200)
Static AllowResize As Boolean
Dim iZoom As Long, T1#, T2#
If AllowResize Then Exit Sub Else AllowResize = 1
If Form.Width < LWidth Then Form.Width = LWidth
If Form.Height < LHeight Then Form.Height = LHeight
T1 = Form.Width / oWidth * 100: T2 = Form.Height / oHeight * 100
iZoom = IIf(T1 > T2, T2, T1)
Form.Zoom = iZoom
AllowResize = 0
End Sub