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

VB.NET-将窗体移动到下一个监视器并最大化,vb.net,forms,windowstate,Vb.net,Forms,Windowstate,有人知道如何将窗体移动到右边的下一个窗口(如果当前监视器是最后一个,则循环)并最大化它吗?我一直在玩,写了一些代码(自学,所以请友好),如果窗体处于“正常”窗口状态,它会移动窗体,但我被困在最大化部分。我本以为WindowState=Maximized就可以了,但是在表单上设置它会导致move函数没有响应 以下是我目前掌握的代码: Module Monitor Public totalMonitors As Integer = System.Windows.Forms.Screen.A

有人知道如何将窗体移动到右边的下一个窗口(如果当前监视器是最后一个,则循环)并最大化它吗?我一直在玩,写了一些代码(自学,所以请友好),如果窗体处于“正常”窗口状态,它会移动窗体,但我被困在最大化部分。我本以为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)。

在移动WindowsState之前,需要将其设置为正常,然后在移动后将其设置回原始状态。我已经将您的代码转换为一个类,这样您就不必担心在移动任何表单之前调用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
公共类监视器
共享子新()
buildMonitorArray()
端接头
公共共享totalMonitors作为整数=System.Windows.Forms.Screen.AllScreens.Count
私有共享XPositionFormMonitors作为新字典(整型、整型)
私有共享YPositionFormMonitors作为新字典(整型、整型)
公共共享子构建MonitorArray()
对于m,整数=0到(totalMonitors-1)
添加(m,System.Windows.Forms.Screen.AllScreens(m.WorkingArea.Location.X)
YPositionFormMonitors.Add(m,System.Windows.Forms.Screen.AllScreens(m.WorkingArea.Location.Y)
下一个
端接头
公共共享子移动外部监视器(targWindow As表单)
Dim newMonitorIndex为整数=identifyCurrentMonitor(targWindow)
Dim originalState=targetWindow.WindowsState
尝试
如果原始状态为WindowsState.Normal,则
targetWindow.WindowsState=FormWindowsState.Normal
如果结束
targetWindow.SetDesktopLocation(XpositionFormMonitors(newMonitorIndex)+1,0)
最后
targetWindow.WindowsState=原始状态
结束尝试
端接头
私有共享函数将CurrentMonitor(targWindow作为窗体)标识为整数
Dim currentMonitorIndex为整数
Dim newMonitorIndex为整数
对于c,整数=0到(totalMonitors-1)
如果targetWindow.Location.X+10>XPositionFormMonitors(c),则
currentMonitorIndex=c
如果结束
下一个
newMonitorIndex=currentMonitorIndex+1
如果newMonitorIndex=totalMonitors,则newMonitorIndex=0
返回newMonitorIndex
端函数
末级

您是否将WindowsState设置为FormWindowsState。在moveToNextMonitor之后最大化?在Windows 7/8中,只有Windows键+Shift键+向右箭头才能移动到右侧的监视器,而Windows键+向上箭头才能最大化。非常感谢用户3308241:)工作得非常出色。在看了代码之后,我有点明白了课堂背后的想法,但是就像我说的。。。自学成才。从哪里开始学习这个OOP最好?这个课程实际上不是一个OOP的好例子,因为所有东西都是共享的。我也是自学成才的,我是从《面向傻瓜的Visual Basic.net》一书开始的。从那以后,我读了很多其他编程书籍,并在软件开发方面取得了巨大的成就。现在,你可以通过网络了解一切。如果你对专业开发软件感兴趣,我建议你学习C#,因为它比vb.net应用得更广泛。祝你好运。