2014-12-08 2 views
1

양식을 다음 창으로 이동하는 방법을 알고 있습니까? 아니면 현재 모니터가 마지막 것일 경우 순환 시키십시오.) 최대화 할 수 있습니까? 나는 놀고 있었고, "보통"windowstate에있는 경우 폼을 움직이는 코드 (스스로 가르쳐 주셨으므로 친절하시기 바랍니다)를 작성했지만 최대화 부분에 붙어 있습니다. 나는 WindowState = Maximized가 그것을 할 것이라고 생각했을 것이다. 그러나 폼에 설정하면 move 함수가 응답하지 않는다.VB.NET - 양식을 다음 모니터로 이동하여 최대화

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 

는 현재, 내가 양식에 moveToNextMonitor (나)를 사용하여 다음 폼로드시 buildMonitorArray 함수를 호출하고있어 : 아래

내가 지금까지 가지고있는 코드입니다.

+0

당신은 moveToNextMonitor 후 FormWindowState.Maximized에 WINDOWSTATE을 설정 했습니까? – Steve

+0

Windows 7/8에서는 Windows 키 + Shift 키 + 오른쪽 화살표를 사용하여 오른쪽 모니터로 이동하고 Windows 키 + 위쪽 화살표를 사용하면 최대화 할 수 있습니다. –

답변

1

이동하기 전에 WindowState를 보통으로 설정 한 다음 이동 한 후에 원래 상태로 다시 설정해야합니다. 폼을 이동하기 전에 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 
+0

고마워요. user3308241 :) 훌륭하게 작동합니다. 나는 코드를보고 수업 뒤에서 아이디어를 얻었지만, 나는 스스로 가르쳤다. 이 OOP를 배우기에 가장 좋은 곳은 어디입니까? –

+0

이 클래스는 실제로 모든 것이 공유되기 때문에 OOP의 좋은 예가 아닙니다. 나는 독학을하고 있으며, Visual Basic.net이라는 책으로 시작했다. 그 이후로 저는 다른 많은 프로그래밍 서적을 읽고 소프트웨어 개발에서 큰 발전을 이루었습니다. 요즘 웹을 통해 모든 것을 배울 수 있습니다. 전문적으로 소프트웨어를 개발하는 데 관심이 있다면 vb.net보다 널리 사용되기 때문에 C#을 배우는 것이 좋습니다. 행운을 빈다. – user3308241

관련 문제