Reputation: 19
Have searched the web a while but didn't find what I was looking for.
Maybe someone can help me. Here's the core code: http://pastebin.com/qWEFrAuC
Upvotes: 1
Views: 2063
Reputation: 996
UPDATE! x2 - picturebox should move with window form.
From what I can know understand, if the mouse leave the form, it will change to the opposite screen monitor (it does for me, though i only have 2 monitors)the amount of monitors you have could be more than 2 but so let me know how you go.
Private Sub Form1_MouseLeave(sender As Object, e As EventArgs) Handles Me.MouseLeave
If Me.DesktopLocation = Screen.AllScreens(1).Bounds.Location Then
Me.DesktopLocation = Screen.AllScreens(0).Bounds.Location + New Point(100, 100)
PictureBox1.Location = New Point(Screen.AllScreens(0).Bounds.Location + New Point(100, 100))
Else
Me.DesktopLocation = Screen.AllScreens(1).Bounds.Location
End If
End Sub
Upvotes: 0
Reputation: 20464
You could loop through the screens to look for the mouse position into their bounds.
This example will center a Form into the screen on which the mouse is currently on, if you want to preserve the position during the window moving, a little bit of effort from your side is required.
To determine the screen:
Dim scr As Screen =
Screen.AllScreens.Where(Function(x) x.Bounds.Contains(Control.MousePosition)).Single
To center the form on the screen:
CenterToScreen(Me, scr)
Public Shared Function CenterToScreen(ByVal f As Form, ByVal display As Screen) As Point
If (display Is Nothing) Then
display = Screen.PrimaryScreen
End If
Dim location As New Point With
{
.X = ((display.Bounds.Width - f.Bounds.Size.Width) \ 2),
.Y = ((display.Bounds.Height - f.Bounds.Size.Height) \ 2)
}
f.Location = location
Return location ' Return the new coordinates of the source Form.
End Function
Note: The function is part of my free API: ElektroKit, where you could get more useful snippets like that one related to the window positioning on Class:
Upvotes: 2
Reputation: 18310
This will put your form in the center of the screen where the mouse pointer is located.
Private Sub CenterToCurrentScreen()
Dim CurrentScreen As Screen = Screen.FromPoint(Cursor.Position)
Me.Location = New Point( _
(CurrentScreen.Bounds.Width / 2) - (Me.Width / 2), _
(CurrentScreen.Bounds.Height / 2) - (Me.Height / 2))
End Sub
For example: calling CenterToCurrentScreen()
when the mouse pointer is on Screen 3 will put your form in the center of Screen 3.
Screen.FromPoint()
will give you the screen at the specified coordinates and Cursor.Position
will give you the coordinates of the mouse pointer.
Upvotes: 0