Version 1.2 Approx 0.6 MB (zipped) First Published 9 Mar 2024 Last Updated 14 Mar 2024
Most forms can be moved by dragging whilst holding down the mouse on the form title bar. However, this isn't possible for a borderless form.
This example app shows how a borderless form can be moved using a mouse down event.
Doing this requires just one line of code:
DragFormWindow Me
The DragFormWindow function uses two simple API calls.
CODE:
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
The attached example app includes all the above code in the module modMoveForm
It contains one form with mouse down events on several form objects. For example:
Private Sub Detail_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
DragFormWindow Me
End Sub
'===========================================================================
Private Sub FormHeader_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
DragFormWindow Me
End Sub
Drag the form to a new position whilst holding down the left mouse button on any blank area in the form header and detail sections.
Areas with no mouse move event (e.g. form footer) do not support form movement by dragging
Videos
Here is a short video (0:07) with no audio demonstrating the code being used:
I have also created a longer video (4:06) for YouTube to explain how this code is used
You can watch the Move Borderless Form Using Mouse Down Event video 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.
Download
Click to download: Move Borderless Form v1.2 ACCDB file Approx 0.6 MB (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.
More Examples
I use this code in several of my example apps available on this website. For example:
Control the Application Interface
Centre Form On Screen
An Attention Seeking Database
The code is particularly useful for borderless forms but it can be used on any type of form.
The second part of this article extends this approach to also allow borderless forms to be resized using a combination of mouse down and mouse move 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 14 Mar 2024
Return to Example Databases Page
Page 1 of 2
1
2
Return To Top
|
|