Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Visual Basic
Expertise: Intermediate
Feb 24, 2000



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

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 Long
Private Declare Function GetWindowLong Lib "user32" _
	Alias "GetWindowLongA" (ByVal hwnd As Long, _
	ByVal nIndex As Long) As Long
Private Const SM_CXFULLSCREEN = 16
Private Const SM_CYFULLSCREEN = 17
Public 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, Top
End Sub
Konstantin Komissarchik
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