views:

50

answers:

2

Scenario: In an Access project a main form has to be positioned and rearranged depending on the size of the Access-Application window. This should be done using VBA.

As far as I know there is no way in Microsoft Access VBA to capture the Restore/Maximize-Event of the Access-Application window (I refer to the Access Window itself not any form inside this).

Is there a way to solve this issue using WIN32 API?

A: 

I don't know of any way to use the WIN32 API to capture the Restore/Maximize Event. The best workaround I can think of is to use the Win32 API in conjunction with the Timer event of a form that is always open (either the Main Menu or some hidden form) and periodically poll the main access window to determine whether it's currently maximized.

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

You'll need to set an interval in the form's TimerInterval property to control how frequently you need to poll the window size.

EDIT: Obviously you'll want to keep track of the main window's previous state so that you don't do any unnecessary processing. The code as posted reflects this.

mwolfe02
A: 

Have a look here:

Move and Resize form windows from code

David-W-Fenton
This is not exactly what I asked for.
Benjamin Brauer
Not exactly, but it is related. For instance, it gives you code for resizing a form to fit the Access client window.
David-W-Fenton