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
See also  5 Tips for Working With an Onsite Interpreter

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist