【问题标题】:VB.NET - Move form to next monitor and maximiseVB.NET - 将表单移动到下一个监视器并最大化
【发布时间】:2014-12-08 22:43:43
【问题描述】:

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

【问题讨论】:

  • 在 moveToNextMonitor 之后是否将 WindowState 设置为 FormWindowState.Maximized?
  • 在 Windows 7/8 中,只需 Windows 键 + Shift 键 + 右箭头移动到右侧的显示器,Windows 键 + 上箭头最大化。

标签: vb.net forms windowstate


【解决方案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

【讨论】:

  • 非常感谢 user3308241 :) 工作出色。在查看代码后,我有点明白课程背后的想法,但就像我说的......自学成才。哪里是开始学习此 OOP 的最佳地点?
  • 这个类实际上并不是 OOP 的一个很好的例子,因为一切都是共享的。我也是自学成才的,我从 Visual Basic.net for dummy 这本书开始。从那时起,我阅读了许多其他编程书籍,并在开发软件方面取得了巨大的成就。如今,您可以通过网络了解几乎所有内容。如果您对专业开发软件感兴趣,我建议您学习 C#,因为它比 vb.net 使用更广泛。祝你好运。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-04
  • 2022-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多