by Dave Hng (4 Submissions)
Category: Custom Controls/Forms/Menus
Compatability: Visual Basic 3.0
Difficulty: Unknown Difficulty
Date Added: Wed 3rd February 2021
Rating: (3 Votes)
Centers a form, relative to the available workspace. This means that if your users have high, or wide taskbars, or other apps which restrict the workspace, your forms will still center properly.
Inputs
General usage:
Stick this in the form's show event:
Center Me
Code Returns
It's a sub, so no returns.
Side Effects
If the workspace is smaller than the form, it still centers, so part of the form will be off the visible area (of course this is a problem with all form centering code).
API DeclarationsPublic Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" (ByVal uAction As Long, ByVal uParam As Long, lpvParam As Any, ByVal fuWinIni As Long) As Long
Global Const SPI_GETWORKAREA As Long = 48
Public Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Public Sub Center(ByRef frm As Form)
'Centers a form, relative to the available workspace
Dim rt As RECT, result As Long
Dim X As Single, Y As Single
Dim oldScaleMode As Integer
result = SystemParametersInfo(SPI_GETWORKAREA, 0&, rt, 0&)
X = rt.Right - rt.Left
Y = rt.Bottom - rt.Top
X = X * Screen.TwipsPerPixelX
Y = Y * Screen.TwipsPerPixelY
X = X \ 2 - (frm.Width \ 2)
Y = Y \ 2 - (frm.Height \ 2)
oldScaleMode = frm.ScaleMode
frm.ScaleMode = vbTwips
frm.Move X, Y
frm.ScaleMode = oldScaleMode
End Sub