Centering A Form - Using Available Desktop Space

Almost everyone who has programmed in VB for even a short period of time, has had to display a form centered on a screen.

A variety of ways exist for doing so, but most ignore some aspects of the environment such as the taskbar, the office launchbar, and even the app's own titlebar.

The following function takes all of this into account to center a form perfectly with the client area.


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 
private Const SM_CYCAPTION = 4  

public Sub CenterForm(Frm as Form)  

    Dim Left, Top as Integer  

    Left = Screen.TwipsPerPixelX * _
      GetSystemMetrics(SM_CXFULLSCREEN) / 2 - Frm.Width / 2 
    Top = Screen.TwipsPerPixelY * _
      (GetSystemMetrics(SM_CYFULLSCREEN) + _
       GetSystemMetrics(SM_CYCAPTION)) / 2 - Frm.Height / 2  

    Frm.Move Left, Top

End Sub  


Comments

  • i know a way quicker way to centre a form

    Posted by 02goodacrel on 12/04/2005 10:04am

    Form1.Left = ((Screen.Width / 2) - (Form1.Width / 2)) Form1.Top = ((Screen.Height / 2) - (Form1.Height / 2))

    Reply
  • Duh - you're all missing the point.

    Posted by Legacy on 11/22/2002 12:00am

    Originally posted by: Steve

    Yes, well done guys. We all know about the screen object. But what my friends in the above posts have failed to spot is that usign the Screen object does not take into account the TaskBar or any other docked windows (like the MS Office Bar or the SmartSuite bar).

    Using the API as described in the original article *IS THE CORRECT WAY* of centering a form in the CLIENT AREA.

    Nuff sed.

    Reply
  • Something Better

    Posted by Legacy on 03/18/2002 12:00am

    Originally posted by: Nesta

    This you can add right into the Form Load Sub..

    Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2

    One line for all your form centering needs.

    Nesta28
    GHB Studios

    Reply
  • COOL

    Posted by Legacy on 08/02/2001 12:00am

    Originally posted by: Benjam�n Mariscal

    It's cool

    Reply
  • Centering a form on the given screen without using API

    Posted by Legacy on 03/30/2000 12:00am

    Originally posted by: Joseba del Arco

    I have found an easiest way to center a form on the screen without using API functions.
    
    

    This is:

    Sub CenterForm(Frm as Form)
    Frm.Move (Screen.Width - Frm.Width)\2, (Screen.Height - Frm.Height)\2
    End Sub

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Targeted attacks and advanced threats are customized to infiltrate your unique IT infrastructure, evade conventional defenses, and remain hidden while stealing your corporate data. To detect these criminal intrusions, analysts and security experts agree that organizations should deploy advanced threat protection as part of an expanded security monitoring strategy. For this comparative analysis of breach detection systems, product analysis reports and comparative analysis reports are used to create the security …

  • On-demand Event Event Date: September 17, 2014 Another day, another end-of-support deadline. You've heard enough about the hazards of not migrating to Windows Server 2008 or 2012. What you may not know is that there's plenty in it for you and your business, like increased automation and performance, time-saving technical features, and a lower total cost of ownership. Check out this webcast and join Rich Holmes, Pomeroy's practice director of virtualization, as he discusses the future state of your servers, …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds