Option Compare Database   'Use database order for string comparisons
Option Explicit

Type EDL_WU_RECT_TYPE
    x1 As Integer
    y1 As Integer
    x2 As Integer
    y2 As Integer
End Type


Declare Function EdlWGetParent Lib "User" Alias "GetParent" (ByVal hwin%) As Integer
Declare Function EdlWGetClassName Lib "User" Alias "GetClassName" (ByVal hwin%, ByVal stBuf$, ByVal cch%) As Integer
Declare Function EdlWGetClientRect Lib "User" Alias "GetClientRect" (ByVal hwin%, rectangle As EDL_WU_RECT_TYPE) As Integer
Declare Function EdlWGetWindowRect Lib "User" Alias "GetWindowRect" (ByVal hwin%, rectangle As EDL_WU_RECT_TYPE) As Integer
Declare Function EdlWGetDC Lib "User" Alias "GetDC" (ByVal hw%) As Integer
Declare Function EdlWGetDeviceCaps Lib "GDI" Alias "GetDeviceCaps" (ByVal hDC%, ByVal iCapability%) As Integer
Declare Function EdlWGetActiveWindow Lib "User" Alias "GetActiveWindow" () As Integer

Const EDLWU_ACCESS = "OMain"
Const EDLWU_ACCESSFRMPOPUP = "OFormPopup"

Const LOGPIXELSX = 88
Const LOGPIXELSY = 90

Function EdlWGetAccessHwnd () As Integer
    
    On Error GoTo EdlWGetAccessHwndError
    
    Dim Hwnd%

    Hwnd% = EdlWGetActiveWindow()
    While ((EdlWWinClass(Hwnd%) <> EDLWU_ACCESS) And (Hwnd% <> 0))
        Hwnd% = EdlWGetParent(Hwnd%)
    Wend
    EdlWGetAccessHwnd = Hwnd%
    Exit Function

EdlWGetAccessHwndError:
    EdlWGetAccessHwnd = 0
    Exit Function
End Function

Function EdlWGetFormDimensions (ByVal FormHwnd%, FormLeft%, FormTop%, FormWidth%, FormHeight%) As Integer
    
    On Error Resume Next
    
    Dim r As EDL_WU_RECT_TYPE, rDesk As EDL_WU_RECT_TYPE
    Dim hwndDesk%, result%
    Dim stClass$

    stClass$ = EdlWWinClass(FormHwnd%)

    ' If this is a popup, use the WindowRect of the desktop.  Otherwise,
    ' use the client rect.
    result% = EdlWGetWindowRect(FormHwnd%, r)
    If (stClass$ = EDLWU_ACCESSFRMPOPUP) Then
        FormLeft% = EdlWPixelsToTwips((r.x1), 0)
        FormTop% = EdlWPixelsToTwips((r.y1), 1)
        FormWidth% = EdlWPixelsToTwips((r.x2 - r.x1), 0)
        FormHeight% = EdlWPixelsToTwips((r.y2 - r.y1), 1)
    Else
        hwndDesk% = EdlWGetParent(FormHwnd%)
        result% = EdlWGetWindowRect(hwndDesk%, rDesk)
        FormLeft% = EdlWPixelsToTwips((r.x1 - rDesk.x1), 0)
        FormTop% = EdlWPixelsToTwips((r.y1 - rDesk.y1), 1)
        FormWidth% = EdlWPixelsToTwips((r.x2 - r.x1), 0)
        FormHeight% = EdlWPixelsToTwips((r.y2 - r.y1), 1)
    End If
    
    EdlWGetFormDimensions = True

End Function

Function EdlWPixelsToTwips (nPixels%, nDirection%) As Integer
    
    On Error GoTo EdlWPixelsToTwipsError
    
    Const nTwipsPerInch = 1440
    Dim nPixelsPerInch%, hDC%

    hDC% = EdlWGetDC(EdlWGetAccessHwnd())
    If (nDirection% = 0) Then       'Horizontal
        nPixelsPerInch% = EdlWGetDeviceCaps(hDC%, LOGPIXELSX)
    Else                            'Vertical
        nPixelsPerInch% = EdlWGetDeviceCaps(hDC%, LOGPIXELSY)
    End If
    EdlWPixelsToTwips = (nPixels% / nPixelsPerInch%) * nTwipsPerInch%
    Exit Function

EdlWPixelsToTwipsError:
    EdlWPixelsToTwips = 0
    Exit Function

End Function

Function EdlWWinClass (Hwnd As Integer) As String
    
    On Error GoTo EdlWWinClassError
    
    
    Const cchMax = 255
    Dim stBuff As String * cchMax
    Dim cch%

    cch% = EdlWGetClassName(Hwnd, stBuff, cchMax)
    EdlWWinClass = (Left$(stBuff, cch%))
    Exit Function

EdlWWinClassError:
    EdlWWinClass = ""
    Exit Function
End Function

Function TestMoveSize (ThisForm As Form) As Integer

    Dim result%, FormLeft%, FormTop%, FormWidth%, FormHeight%
    
    result% = EdlWGetFormDimensions(ThisForm.Hwnd, FormLeft%, FormTop%, FormWidth%, FormHeight%)
    DoCmd MoveSize FormLeft%, FormTop%, FormWidth%, FormHeight%

End Function

