Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Visual Basic
Expertise: Intermediate
Jul 16, 1998



Building the Right Environment to Support AI, Machine Learning and Deep Learning

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 Explicit
Private Const SPI_GETWORKAREA = 48
Private 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 Long
End Type

Public 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 + ScreenTop
End Function
Miguel Santos
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date