Version 1.2 Approx 0.7 MB (zipped) First Published 15 June 2024
This is a follow-up to my first article in this series.
Soon after posting about the earlier article and video on LinkedIn, fellow MVP, Karl Donaubauer wrote the following comment:
So, the standard form moves with the main window, the popup doesn't. However, last week a poster (Paula) in my little German forum asked if it's possible to drag the main window together with the popup forms of her application when she wants to move both to a different screen. How about that challenge? 😎
Although Karl's challenge was slightly tongue-in-cheek, I accepted the challenge knowing it was very easy to do!
In fact it took less than 10 minutes both to implement this and to respond on LinkedIn. Documenting the changes needed on this web page took significantly longer!
Download:
MoveAppWindow+PopupForm_v1.2 (ACCDB - zipped)
Download and unblock the zip file. For more details, see my article: Unblock downloaded files by removing the Mark of the Web
Unzip and save the ACCDB file to a trusted location.
Using the Example App
The startup form and standard form are identical to those in the example app supplied with the first part of this article.
See the previous article for an explanation of the original code used.
The popup form has been modified to include additional code imported from my Move a Borderless Form example app
As before, holding the left mouse button over the small Access icon button whilst you drag the form will move the application window in the direction specified.
However, the form will be left in its current position. This happens because popup forms are independent of the application window.
You can of course move the form itself without any code by dragging on the form title bar
You can also do this using code by dragging the form whilst holding down the left mouse button over the Popup Form label in the form header.
In each case, the form will move leaving the application window unchanged
Finally, hold down the left mouse button on any blank space in the form header.
This time both the application window and the popup form will move the same distance in the direction specified.
NOTE:
In fact, the form moves first with the application window replicating that movement immediately afterwards.
To achieve this effect, the mouse down/mouse move events use a combination of both the previous sets of code. QED!
There is no need to use all three sets of code events in your own forms. Just use whichever you need!
Form Code (to move the application window ONLY)
Private Sub cmdMoveWindow_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
'select application window
AppWindowSelect
End Sub
' --------------------------------------
Private Sub cmdMoveWindow_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
'drag application window
If Button = 0 Then Exit Sub
AppWindowMove
End Sub
Form Code (to move the popup form ONLY)
Private Sub lblHeader_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
'drag the form
DragFormWindow Me
End Sub
Form Code (to move the popup form AND the application window)
Private Sub FormHeader_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
'select application window
AppWindowSelect
End Sub
' --------------------------------------
Private Sub FormHeader_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
'drag the form
DragFormWindow Me
'move application window replicating the distance/direction moved
If Button = 0 Then Exit Sub
AppWindowMove
End Sub
The underlying module code in modAppWindow is identical to that in the earlier article except for adding code to change the mouse cursor
Module Code (modAppWindow):
Option Compare Database
Option Explicit
'-------------------------------------------------------
' Module modAppWindow
' DateTime : 17/02/2024
' Authors : Colin Riddington / Jesús Mansilla
' Website : https://www.isladogs.co.uk
' Purpose : Code to move the application window using mouse down / mouse move events
' Copyright : The code in the utility MAY be altered and reused in your own applications
' provided the copyright notice is left unchanged (including Author, Website and Copyright)
' You are NOT allowed to sell, resell or repost this on other sites such as online forums
' without permission from the author. However, links back to the above websites ARE allowed.
' If you find this code useful please place a link to my website on your own web site
' so that others may benefit as well.
' Updated : March 2024.
'-------------------------------------------------------
'APIs used for moving application window
Declare PtrSafe Function SetWindowPos Lib "user32" (ByVal hwnd As LongPtr, ByVal hWndInsertAfter As LongPtr, _
ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Declare PtrSafe Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Public Declare PtrSafe Function GetWindowRect Lib "user32" (ByVal hwnd As LongPtr, lpRect As RECT) As Long
Public Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Public Type POINTAPI
X As Long
Y As Long
End Type
Const HWND_TOP = 0 'Moves MS Access window to top of Z-order
Const SWP_NOZORDER = &H4 'wFlags: Ignores the hWndInsertAfter
Const SWP_NOMOVE = &H2 'wFlags: don't change the window position
Const SWP_NOSIZE = &H1 'wFlags: don't change the window size
Dim AppX As Long, AppY As Long, AppTop As Long, AppLeft As Long, WinRECT As RECT, APointAPI As POINTAPI
'-------------------------------------------------------
Sub AppWindowSelect()
'select application window
GetWindowRect Application.hWndAccessApp, WinRECT
AppTop = WinRECT.Top
AppLeft = WinRECT.Left
GetCursorPos APointAPI
AppX = APointAPI.X
AppY = APointAPI.Y
'change cursor to crosshair
ChangeCursorTo (IDC_SIZEALL)
End Sub
'-------------------------------------------------------
Sub AppWindowMove()
'drag application window
GetCursorPos APointAPI
SetWindowPos Application.hWndAccessApp, HWND_TOP, AppLeft - (AppX - APointAPI.X), _
AppTop - (AppY - APointAPI.Y), 0, 0, SWP_NOZORDER + SWP_NOSIZE
End Sub
The DragFormWindow function uses two simple API calls in module modMoveForm
Module Code (modMoveForm):
Option Compare Database
Option Explicit
Public Const WM_NCLBUTTONDOWN = &HA1
Public Const HT_CAPTION = &H2
' 32/64 Bit Windows API calls for VBA 7 (A2010 or later)
'https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-sendmessage
'Sends the specified message to a window or windows.
'The SendMessage function calls the window procedure for the specified window
'and does not return until the window procedure has processed the message.
Public Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As LongPtr, _
ByVal wMsg As Long, ByVal wParam As LongPtr, lParam As Any) As LongPtr
'https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-releasecapture
'Releases the mouse capture from a window in the current thread and restores normal mouse input processing.
Public Declare PtrSafe Function ReleaseCapture Lib "user32.dll" () As Long
'===========================================================================
'Use the following code in a mouse down event:
'DragFormWindow Me
Public Function DragFormWindow(frm As Form)
With frm
ReleaseCapture
SendMessage .hWnd, WM_NCLBUTTONDOWN, HT_CAPTION, 0
End With
End Function
Finally the ChangeCursorTo code to modify the mouse cursor appearance is in module modChangeCursor
Module Code (modMouseCursor):
Option Compare Database
Option Explicit
'http://www.vbaexpress.com/kb/getarticle.php?kb_id=929
'Declarations for API Functions
Private Declare PtrSafe Function LoadCursor Lib "user32" Alias "LoadCursorA" (ByVal hInstance As LongPtr, ByVal lpCursorName As Long) As LongPtr
Private Declare PtrSafe Function SetCursor Lib "user32" (ByVal hCursor As LongPtr) As LongPtr
'Declare Windows API Constants for Windows System cursors
Public Const GCW_HCURSOR As Long = (-12)
Public Const IDC_APPSTARTING = 32650&
Public Const IDC_HAND = 32649&
Public Const IDC_ARROW = 32512&
Public Const IDC_CROSS = 32515&
Public Const IDC_IBEAM = 32513&
Public Const IDC_ICON = 32641&
Public Const IDC_NO = 32648&
Public Const IDC_SIZE = 32640&
Public Const IDC_SIZEALL = 32646&
Public Const IDC_SIZENESW = 32643&
Public Const IDC_SIZENS = 32645&
Public Const IDC_SIZENWSE = 32642&
Public Const IDC_SIZEWE = 32644&
Public Const IDC_UPARROW = 32516&
Public Const IDC_WAIT = 32514&
'===========================================================================
Public Sub ChangeCursorTo(ByVal lngCursor As Long)
SetCursor LoadCursor(0&, lngCursor)
Public Sub ResetCursor()
SetCursor LoadCursor(0&, IDC_ARROW)
End Sub
Video
I have created a short video (2:22) to demonstrate how popup forms can be moved together with the application window
You can watch the video Move the Application Window AND Popup Form Using Code on my Isladogs YouTube channel or you can click below:
If you liked the video, please subscribe to my Isladogs on Access channel on YouTube. Thanks.
Related Articles
Control the Application Interface This shows how the Access application interface can be controlled e.g. hide the Access UI leaving forms 'floating on the desktop'
Move and Resize Borderless Form This shows how borderless forms can be moved and resized using mouse events
Feedback
Please use the contact form below to let me know whether you found this article interesting/useful or if you have any questions/comments.
Please also consider making a donation towards the costs of maintaining this website. Thank you
Colin Riddington Mendip Data Systems Last Updated 15 June 2024
Return to Example Databases Page
Page 2 of 2
1
2
Return To Top
|
|