devxlogo

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

Charlie has over a decade of experience in website administration and technology management. As the site admin, he oversees all technical aspects of running a high-traffic online platform, ensuring optimal performance, security, and user experience.

See also  Five Early Architecture Decisions That Quietly Get Expensive

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.