Yet Another CenterForm Routine

Yet Another CenterForm Routine

In the April 1997 issue of VBPJ, you published a tip called “Consider the Taskbar When Centering Forms.” You can center forms more easily with the SystemParametersInfo API call:

 Private Declare Function _	SystemParametersInfo Lib "user32" Alias _	"SystemParametersInfoA" (ByVal uAction _	As Long, ByVal uParam As Long, R As Any, _	ByVal fuWinIni As Long) As LongPrivate Type RECT	Left As Long	Top As Long	Right As Long	Bottom As LongEnd TypePrivate Const SPI_GETWORKAREA = 48Public Sub CenterForm(frm As Form)	Dim R As RECT, lRes As Long, 	Dim lW As Long, lH As Long	lRes = SystemParametersInfo( _		SPI_GETWORKAREA, 0, R, 0)	If lRes Then		With R			.Left = Screen.TwipsPerPixelX * .Left			.Top = Screen.TwipsPerPixelY * .Top			.Right = Screen.TwipsPerPixelX * .Right			.Bottom = Screen.TwipsPerPixelY * .Bottom			lW = .Right - .Left			lH = .Bottom - .Top			frm.Move .Left + (lW - frm.Width)  2, _				.Top + (lH - frm.Height)  2		End With	End IfEnd Sub
See also  Essential Measures for Safeguarding Your Digital Data

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