Function to save/restore last used form size/position with multiple monitor support

Darren Rose 311 Reputation points
2023-09-17T15:39:06.7733333+00:00

Can anyone recommend a good VB / C# function I can use to save/restore windows form size/position with multiple monitor support which covers issues like second monitor then being disconnected etc.

Have googled but most solutions I find have issues and are quite old and want to find a recommended / reliable solution, and I respect the opinion of those on here so thought I would ask.

Thank you

Developer technologies Windows Forms
Developer technologies VB
Developer technologies C#
{count} votes

2 answers

Sort by: Most helpful
  1. Dewayne Basnett 1,381 Reputation points
    2023-09-18T15:33:04.2333333+00:00

    To answer the "...which covers issues like second monitor then being disconnected..." question.

    I've had this issue. All of our production Form apps have a common splash screen that always shows. On that screen I put a button to fix the application not being visible when started. The button sets a boolean that the main form checks in the Load event, and if true it sets / resets, the location.

    The user can adjust the monitor for the app window immediately after starting by,

    Win + Shift + Left Arrow: move a window to the monitor on the left

    Win + Shift+Right Arrow: move a window to the monitor on the right

    1 person found this answer helpful.

  2. Darren Rose 311 Reputation points
    2023-09-18T13:56:05.0766667+00:00

    Final solution which seems to work for me:

    My.Settings

    2023-09-18_14-49-48

    Code

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            ' Restore Form Size, Location and WindowState - override by holding down Ctrl+Shift when opening app
            If (Control.ModifierKeys And Keys.Shift) = 0 Then
                If IsOnScreen(My.Settings.FormLocation, My.Settings.FormSize) Then
                    Size = My.Settings.FormSize
                    Location = My.Settings.FormLocation
                    WindowState = My.Settings.FormWindowState
                Else
                    Size = My.Settings.FormSize
                    WindowState = My.Settings.FormWindowState
                End If
            End If
    
    End Sub
    
    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
    
            ' Save Form Size, Location and WindowState
            ' Save Size and Location (or RestoreBounds Size and Location if Minimized or Maximized)
            If Me.WindowState = FormWindowState.Normal Then
                My.Settings.FormSize = Size
                My.Settings.FormLocation = Location
            Else
                My.Settings.FormSize = RestoreBounds.Size
                My.Settings.FormLocation = RestoreBounds.Location
            End If
            ' Only save the WindowState if Normal or Maximized
            If Me.WindowState = FormWindowState.Normal Or Me.WindowState = FormWindowState.Maximized Then
                My.Settings.FormWindowState = WindowState
            Else
                My.Settings.FormWindowState = FormWindowState.Normal
            End If
    
            ' save settings
            My.Settings.Save()
    
    End Sub
    
    Public Function IsOnScreen(ByVal RecLocation As System.Drawing.Point, ByVal RecSize As System.Drawing.Size, Optional ByVal MinPercentOnScreen As Double = 0.2) As Boolean
            Dim PixelsVisible As Double = 0
            Dim Rec As New System.Drawing.Rectangle(RecLocation, RecSize)
    
            For Each Scrn As Screen In Screen.AllScreens
                Dim r As System.Drawing.Rectangle = System.Drawing.Rectangle.Intersect(Rec, Scrn.WorkingArea)
                ' intersect rectangle with screen
                If r.Width <> 0 And r.Height <> 0 Then
                    PixelsVisible += (r.Width * r.Height)
                    ' tally visible pixels
                End If
            Next Scrn
            Return PixelsVisible >= (Rec.Width * Rec.Height) * MinPercentOnScreen
    End Function
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.