프로그래밍

[vb.net] move form to another monitor

프로세스 천국 2013. 4. 4. 20:33

Public Class MovableForm
    Inherits Form

    Public Sub New()
        Me.Text = "Movable Form"
        Me.StartPosition = FormStartPosition.CenterScreen

        Dim btn As New Button()
        btn.Text = "To another monitor!"
        AddHandler btn.Click, AddressOf ButtonOnClick

        Me.Controls.Add(btn)

        Me.Show()
    End Sub

    Private Sub ButtonOnClick(ByVal sender As Object, ByVal e As EventArgs)
        ' Get all screens, then determine what screen the form is currently on
        Dim allScreens As Screen() = Screen.AllScreens
        Dim currentScreen As Screen = Screen.FromControl(Me)
        Dim currentIndex As Integer = Array.IndexOf(allScreens, currentScreen)


        ' Move to another screen, indicate the index in the title bar, and get a reference
        ' to the new screen
        Dim newScreenIndex As Integer = (currentIndex + 1) Mod allScreens.Length
        Me.Text = "Movable Form (" & newScreenIndex & ")"
        Dim newScreen As Screen = allScreens(newScreenIndex)

        ' This is the area of the screen the form can use: total bounds minus any
        ' toolbars, taskbar, etc.
        Dim newBounds As Rectangle = newScreen.WorkingArea


        ' This is the center of the new screen's working area
        Dim centerPoint = New Point((newBounds.Width - Me.Width) \ 2, (newBounds.Height - Me.Height) \ 2)

        ' Now, we adjust to global coordinates by adding the new screen's location
        Dim finalPoint As Point = New Point(centerPoint.X + newBounds.X, centerPoint.Y + newBounds.Y)

        ' Move the form
        Me.Location = finalPoint
    End Sub

End Class

 

출처는 어딘지 모름. 하도 오래된거라서..