场景:在Access项目中,必须根据Access-Application窗口的大小定位和重新排列主窗体。这应该使用VBA来完成。
据我所知,Microsoft Access VBA无法捕获Access-Application窗口的Restore / Maximize-Event(我在其中指的是Access Window本身并不是任何形式)。
有没有办法使用WIN32 API解决这个问题?
答案 0 :(得分:3)
我不知道如何使用WIN32 API捕获Restore / Maximize 事件。我能想到的最佳解决方法是将Win32 API与始终打开的表单的Timer事件(主菜单或某些隐藏表单)结合使用,并定期轮询主访问窗口以确定它当前是否已最大化。
Enum WindowSize
wsMax = 1
wsMin
wsRestore
End Enum
'Functions return 1 for true and 0 for false; multiply result by -1 to use as Boolean'
Private Declare Function IsZoomed Lib "User32" (ByVal hWnd As Long) As Integer
Private Declare Function IsIconic Lib "User32" (ByVal hWnd As Long) As Integer
Function IsMaximized(hWnd As Long) As Boolean
IsMaximized = IsZoomed(hWnd) * -1
End Function
Function IsMinimized(hWnd As Long) As Boolean
IsMinimized = IsIconic(hWnd) * -1
End Function
Private Sub Form_Timer()
Static PrevWinSize As WindowSize
If IsMaximized(hWndAccessApp) Then
If PrevWinSize <> wsMax Then
'Window has been maximized since we last checked'
MsgBox "Main Access window is maximized"
PrevWinSize = wsMax
End If
ElseIf IsMinimized(hWndAccessApp) Then
If PrevWinSize <> wsMin Then
'Window has been minimized since we last checked'
MsgBox "Main Access window is minimized"
PrevWinSize = wsMin
End If
Else
If PrevWinSize <> wsRestore Then
'Window has been restored since we last checked'
MsgBox "Main Access window is restored"
PrevWinSize = wsRestore
End If
End If
End Sub
您需要在表单的TimerInterval属性中设置一个间隔,以控制轮询窗口大小所需的频率。
编辑:显然,您需要跟踪主窗口的先前状态,这样您就不会进行任何不必要的处理。发布的代码反映了这一点。