1

有谁知道如何将表单移动到右侧的下一个窗口(或者如果当前监视器是最后一个则循环)并最大化它?我一直在玩并编写了一些代码(自学,所以请善待),如果它处于“正常”窗口状态,它会移动表单,但我被困在最大化部分。我原以为 WindowState = Maximized 会这样做,但是在表单上设置它会导致移动功能没有响应。

以下是我到目前为止的代码:

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) 。

4

1 回答 1

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
于 2014-12-08T23:48:42.010 回答