Results 1 to 2 of 2

Thread: [2008] Resize and center from on primary screen

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2008
    Posts
    515

    [2008] Resize and center from on primary screen

    Depending on end users primary screen resolution, I size my form.

    Code:
    Me.Size = New System.Drawing.Size((workingRectangle.Width - 200), (workingRectangle.Height - 200))
    After resizing I would like to center the form on the primary screen

    Please can someone show me how to do this ?

    Me.StartPosition = FormStartPosition.CenterScreen does not center it.

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2008
    Posts
    515

    Re: [2008] Resize and center from on primary screen

    Answering my own question:
    vb Code:
    1. Imports System.Windows.Forms.Screen

    Code:
                ' Retrieve the working rectangle from the Screen class
                ' using the PrimaryScreen and the WorkingArea properties.
                Dim workingRectangle As System.Drawing.Rectangle = Screen.PrimaryScreen.WorkingArea
                ' Set the size of the form slightly less than size of 
                ' working rectangle.
                Me.Size = New System.Drawing.Size((workingRectangle.Width - 200), (workingRectangle.Height - 200))
                ' Set the location so the entire form is visible.
                '       Me.Location = New System.Drawing.Point(5, 5)
    
                Dim allScreens As Screen() = Screen.AllScreens
                Dim currentScreen As Screen = Screen.FromControl(Me)
                Dim currentIndex As Integer = Array.IndexOf(allScreens, currentScreen)
    
                Dim PrimaryScreen As Screen = allScreens(0)
    
                ' This is the area of the screen the form can use: total bounds minus any
                ' toolbars, taskbar, etc.
                Dim newBounds As Rectangle = PrimaryScreen.WorkingArea
    
                Dim centerPoint = New Point((newBounds.Width - Me.Width) \ 2, (newBounds.Height - Me.Height) \ 2)
    
                ' Now, we adjust to global coordinates by adding the new screen's location
                Dim finalPoint As Point = New Point(centerPoint.X + newBounds.X, centerPoint.Y + newBounds.Y)
    
                ' Move the form
                Me.Location = finalPoint

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width