VB.NET - 将表单移动到下一个监视器并最大化

时间:2014-12-08 22:43:44

标签: vb.net forms windowstate

有没有人知道如何将表单移动到右边的下一个窗口(如果当前监视器是最后一个窗口,则循环)并最大化它?我一直在玩,写一些代码(自学,所以请善待),如果它在" Normal" windowstate,但我坚持最大化部分。我原以为WindowState = Maximized会这样做,但在表单上设置它会导致move函数没有响应。

以下是我目前的代码:

Module Monitor

    Public totalMonitors As Integer = System.Windows.Forms.Screen.AllScreens.Count

    Private xPositionForMonitors As New Dictionary(Of Integer, Integer)
    Private yPositionForMonitors As New Dictionary(Of Integer, Integer)
    Private currentMonitorIndex As Integer
    Private newMonitorIndex As Integer

    Public Sub buildMonitorArray()

        For m As Integer = 0 To (totalMonitors - 1)
            xPositionForMonitors.Add(m, System.Windows.Forms.Screen.AllScreens(m).WorkingArea.Location.X)
            yPositionForMonitors.Add(m, System.Windows.Forms.Screen.AllScreens(m).WorkingArea.Location.Y)
        Next

    End Sub

    Public Sub moveToNextMonitor(targWindow As Form)

        identifyCurrentMonitor(targWindow)
        targWindow.SetDesktopLocation(xPositionForMonitors(newMonitorIndex) + 1, 0)

    End Sub

    Private Sub identifyCurrentMonitor(targWindow As Form)

        For c As Integer = 0 To (totalMonitors - 1)
            If targWindow.Location.X + 10 > xPositionForMonitors(c) Then
                currentMonitorIndex = c
            End If
        Next

        newMonitorIndex = currentMonitorIndex + 1
        If newMonitorIndex = totalMonitors Then newMonitorIndex = 0

    End Sub

End Module

目前,我在表单加载时调用buildMonitorArray函数,然后在表单上使用moveToNextMonitor(Me)。

1 个答案:

答案 0 :(得分:1)

在移动之前需要将WindowState设置为Normal,然后在移动后将其设置回原始状态。我已将您的代码转换为类,以便您在移动任何表单之前不必担心调用buildMonitorArray方法。要调用您的方法,您需要调用Monitor.moveToNextMonitor,因为它现在是一个类。如果您仍然喜欢使用模块,那么您只需将代码更改移植到模块中,它仍然可以以相同的方式工作。

Public Class Monitor

Shared Sub New()
    buildMonitorArray()
End Sub

Public Shared totalMonitors As Integer = System.Windows.Forms.Screen.AllScreens.Count

Private Shared xPositionForMonitors As New Dictionary(Of Integer, Integer)
Private Shared yPositionForMonitors As New Dictionary(Of Integer, Integer)

Public Shared Sub buildMonitorArray()
    For m As Integer = 0 To (totalMonitors - 1)
        xPositionForMonitors.Add(m, System.Windows.Forms.Screen.AllScreens(m).WorkingArea.Location.X)
        yPositionForMonitors.Add(m, System.Windows.Forms.Screen.AllScreens(m).WorkingArea.Location.Y)
    Next
End Sub

Public Shared Sub moveToNextMonitor(targWindow As Form)
    Dim newMonitorIndex As Integer = identifyCurrentMonitor(targWindow)
    Dim originalState = targWindow.WindowState
    Try
        If originalState <> FormWindowState.Normal Then
            targWindow.WindowState = FormWindowState.Normal
        End If
        targWindow.SetDesktopLocation(xPositionForMonitors(newMonitorIndex) + 1, 0)
    Finally
        targWindow.WindowState = originalState
    End Try
End Sub

Private Shared Function identifyCurrentMonitor(targWindow As Form) As Integer
    Dim currentMonitorIndex As Integer
    Dim newMonitorIndex As Integer
    For c As Integer = 0 To (totalMonitors - 1)
        If targWindow.Location.X + 10 > xPositionForMonitors(c) Then
            currentMonitorIndex = c
        End If
    Next

    newMonitorIndex = currentMonitorIndex + 1
    If newMonitorIndex = totalMonitors Then newMonitorIndex = 0
    Return newMonitorIndex
End Function

End Class