devxlogo

Center Forms with Taskbar Visible

Center Forms with Taskbar Visible

Just about every VB developer uses the Move (Screen.Width – Width) 2, (Screen.Height – Height) 2 method to center the forms on screen. However, when the user has the Windows 95 or NT 4.0 taskbar visible, your form centers on screen but doesn’t take into account the position of the taskbar itself. The CenterForm32 routine centers a form in available screen area, taking into account the taskbar. Add this code to the Declarations section of a module, and put the code CenterForm32 Me on the Form_Load event of the forms you want to center:

 Option ExplicitPrivate Const SPI_GETWORKAREA = 48Private Declare Function _	SystemParametersInfo& Lib "User32" _	Alias "SystemParametersInfoA" ( _	ByVal uAction As Long, _	ByVal uParam As Long, lpvParam As Any, _	ByVal fuWinIni As Long)Private Type RECT	Left As Long	Top As Long	Right As Long	Bottom As LongEnd TypePublic Function CenterForm32 (frm As Form)	Dim ScreenWidth&, ScreenHeight&, _		ScreenLeft&, ScreenTop&	Dim DesktopArea As RECT	Call SystemParametersInfo ( _		SPI_GETWORKAREA, 0, DesktopArea, 0)	ScreenHeight = (DesktopArea.Bottom - _		DesktopArea.Top) * Screen.TwipsPerPixelY	ScreenWidth = (DesktopArea.Right - _		DesktopArea.Left) * Screen.TwipsPerPixelX	ScreenLeft = DesktopArea.Left * Screen.TwipsPerPixelX	ScreenTop = DesktopArea.Top * Screen.TwipsPerPixelY	frm.Move (ScreenWidth - frm.Width)  _		 2 + ScreenLeft, (ScreenHeight - _		frm.Height)  2 + ScreenTopEnd Function
devxblackblue

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