Hi All,
I am trying to do what I thought was just a simple resize of an Access application window to a form. To test this I tried the SetWindowPos and MoveWindow with just some random values that I know would work to resize the window. However, neither API function
is working.
SetWindowPos doesn't do anything at all, and MoveWindow sets the Left to 32676, Top to 326627, Width to 32767, and Height to 33867. At this point the application window disappears and I can't get it back without restarting Access.
Below is my code. I am new to the Windows API thing, so I apologize in advance if it is some dumb rookie mistake. This is on Access 2016.
Below are my declarations:
#If VBA7 Then
Public Declare PtrSafe Function GetSystemMetrics Lib "user32" (ByVal nIndex As LongPtr) As Long
Public Declare PtrSafe Function GetDC Lib "user32" (ByVal hWnd As LongPtr) As LongPtr
Public Declare PtrSafe Function GetDeviceCaps Lib "gdi32" (ByVal hDC As LongPtr, ByVal nIndex As Long) As Long
Public Declare PtrSafe Function ReleaseDC Lib "user32" (ByVal hWnd As LongPtr, ByVal hDC As LongPtr) As Long
Public Declare PtrSafe Function GetWindowRect Lib "user32" (ByVal hWnd As LongPtr, ByRef lpRect As RECT) As Long
Public Declare PtrSafe Function MoveWindow Lib "user32" (ByVal hWnd As LongPtr, X As Long, Y As Long, nWidth As Long, nHeight As Long, bRepaint As Boolean) As Boolean
Public Declare PtrSafe Function SetWindowPos Lib "user32" (ByVal hWnd As LongPtr, hWndInsertAfter As LongPtr, X As Long, Y As Long, cx As Long, cy As Long, uFlags As Long) As Boolean
#Else
Public Declare Function GetDC Lib "user32" (ByVal hWnd As Long) As Long
Public Declare Function GetDeviceCaps Lib "gdi32" (ByVal hDC As Long, ByVal nIndex As Long) As Long
Public Declare Function ReleaseDC Lib "user32" (ByVal hWnd As Long, ByVal hDC As Long) As Long
Public Declare Function GetWindowRect Lib "user32" (ByVal hWnd As Long, ByRef lpRect As RECT) As Long
Public Declare Function MoveWindow Lib "user32" (ByVal hWnd As Long, X As Long, Y As Long, nWidth As Long, nHeight As Long, bRepaint As Boolean) As Boolean
Public Declare Function SetWindowPos Lib "user32" (ByVal hWnd As Long, hWndInsertAfter As Long, X As Long, Y As Long, cx As Long, cy As Long, uFlags As Long) As Boolean
#End If
Public Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
And below is my simple test code that doesn't work
Public Sub SizeAppWindow()
Dim newAppWindow As RECT
Dim appHandle As Long
appHandle = Application.hWndAccessApp
MoveWindow appHandle, 0, 0, 800, 600, 1
'SetWindowPos appHandle, 0, 0, 0, 700, 1000, 0
Application.RefreshDatabaseWindow
WindowsAPICode.GetWindowRect appHandle, newAppWindow
Debug.Print newAppWindow.Left & " " & newAppWindow.Right & " " & newAppWindow.Top & " " & newAppWindow.Bottom
End Sub
The GetWindowRect in the immediate window prints the following
With SetWindowPos:
0 1920 0 1050 (just my monitor size since Access window is maximized)
With MoveWindow:
32767 36627 32767 33867
Any ideas?