Public Class Form1
Private seconfForm As Form = New Form2
Private Function TryGetWindowRect(ByRef rect) As Boolean
If rect <> Rectangle.Empty Then
Dim screenRect As Rectangle = Nothing
Dim allScreens As Screen() = Screen.AllScreens
For i As Integer = 0 To allScreens.Length - 1
Dim scr As Screen = allScreens(i)
screenRect = Rectangle.Union(screenRect, scr.Bounds)
Next
Dim ht As Integer = screenRect.Height
Dim w As Integer = screenRect.Width
Dim x As Integer = screenRect.X
Dim y As Integer = screenRect.Y
Dim topleft As Point = New Point(Math.Max(rect.Left, x), Math.Max(rect.Top, y))
Dim bottomRight As Point = New Point(Math.Min(rect.Right, x + w), Math.Min(rect.Bottom, y + ht))
rect.Location = topleft
rect.Height = Math.Abs(bottomRight.Y - rect.Top)
rect.Width = Math.Abs(bottomRight.X - rect.Left)
Return True
End If
rect = Rectangle.Empty
Return False
End Function
Private Sub Form1_LocationChanged(sender As Object, e As EventArgs) Handles MyBase.LocationChanged
Dim windowBounds As Rectangle = BoundsToScreen(Me.Bounds)
Dim pt As Point = New Point(windowBounds.Left + windowBounds.Width, windowBounds.Top)
If seconfForm IsNot Nothing Then
seconfForm.Location = pt
seconfForm.Bounds = New Rectangle(pt, New Size(windowBounds.Size.Width - 8, windowBounds.Size.Height - 8))
End If
End Sub
Private Sub Form1_SizeChanged(sender As Object, e As EventArgs) Handles MyBase.SizeChanged
Dim windowBounds As Rectangle = BoundsToScreen(Me.Bounds)
Dim pt As Point = New Point(windowBounds.Left + windowBounds.Width, windowBounds.Top)
If seconfForm IsNot Nothing Then
seconfForm.Location = pt
seconfForm.Bounds = New Rectangle(pt, New Size(windowBounds.Size.Width - 8, windowBounds.Size.Height - 8))
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
seconfForm.FormBorderStyle = FormBorderStyle.None
seconfForm.Show()
Dim windowBounds As Rectangle = BoundsToScreen(Me.Bounds)
Dim pt As Point = New Point(windowBounds.Left + windowBounds.Width, windowBounds.Top)
If seconfForm IsNot Nothing Then
seconfForm.Location = pt
seconfForm.Bounds = New Rectangle(pt, New Size(windowBounds.Size.Width - 8, windowBounds.Size.Height - 8))
End If
End Sub
Private Function BoundsToScreen(ByRef rect) As Rectangle
Dim screenRect As Rectangle = Nothing
Dim allScreens As Screen() = Screen.AllScreens
For i As Integer = 0 To allScreens.Length - 1
Dim scr As Screen = allScreens(i)
screenRect = Rectangle.Union(screenRect, scr.Bounds)
Next
Dim ht As Integer = screenRect.Height
Dim w As Integer = screenRect.Width
Dim x As Integer = screenRect.X
Dim y As Integer = screenRect.Y
Dim topleft As Point = New Point(Math.Max(rect.Left, x), Math.Max(rect.Top, y))
Dim bottomRight As Point = New Point(Math.Min(rect.Right, x + w), Math.Min(rect.Bottom, y + ht))
rect.Location = topleft
rect.Height = Math.Abs(bottomRight.Y - rect.Top)
rect.Width = Math.Abs(bottomRight.X - rect.Left)
Return rect
End Function
End Class