Centering A Form - Using Available Desktop Space
Posted
by Konstantin Komissarchik
on January 28th, 2004
A variety of ways exist for doing so, but most ignore some aspects of the environment such as the taskbar, the office launchbar, and even the app's own titlebar.
The following function takes all of this into account to center a form perfectly with the client area.
private Declare Function GetSystemMetrics Lib "user32" _
(byval nIndex as Long) as Long
private Declare Function GetWindowLong Lib "user32" alias _
"GetWindowLongA" (byval hwnd as Long, _
byval nIndex as Long) as Long
private Const SM_CXFULLSCREEN = 16
private Const SM_CYFULLSCREEN = 17
private Const SM_CYCAPTION = 4
public Sub CenterForm(Frm as Form)
Dim Left, Top as Integer
Left = Screen.TwipsPerPixelX * _
GetSystemMetrics(SM_CXFULLSCREEN) / 2 - Frm.Width / 2
Top = Screen.TwipsPerPixelY * _
(GetSystemMetrics(SM_CYFULLSCREEN) + _
GetSystemMetrics(SM_CYCAPTION)) / 2 - Frm.Height / 2
Frm.Move Left, Top
End Sub

Comments
There are no comments yet. Be the first to comment!