devxlogo

Account for Taskbars When Centering Forms

Account for Taskbars When Centering Forms

Most VB programmers must display a form centered on a screen. You can do this in a variety of ways, but most ignore aspects of the environment such as the taskbar or the office launchbar. This function takes these aspects into account to center a form within the client area perfectly:

 Private Declare Function GetSystemMetrics Lib "user32" _	(ByVal nIndex As Long) As LongPrivate Declare Function GetWindowLong Lib "user32" _	Alias "GetWindowLongA" (ByVal hwnd As Long, _	ByVal nIndex As Long) As LongPrivate Const SM_CXFULLSCREEN = 16Private Const SM_CYFULLSCREEN = 17Public Sub CenterForm(Frm As Form)	Dim Left As Long, Top As Long	Left = (Screen.TwipsPerPixelX _		* (GetSystemMetrics(SM_CXFULLSCREEN) / 2)) - _		(Frm.Width / 2)	Top = (Screen.TwipsPerPixelY * _		(GetSystemMetrics(SM_CYFULLSCREEN) / 2)) - _		(Frm.Height / 2)	Frm.Move Left, TopEnd Sub

devx-admin

Share the Post: