Reputation: 1393
Here is my code:
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseMove
If (e.Button = Windows.Forms.MouseButtons.Left) Then
Me.PictureBox1.Location = New Point((Me.pbpos.X + (Control.MousePosition.X - Me.offset.X)), _
(Me.pbpos.Y + (Control.MousePosition.Y - Me.offset.Y)))
End If
End Sub
This is the picture, not yet dragged. Ofcourse it does not fit the screen, although the top and the left side is the edges of the picture. (the gray color is the panel)
Now if I drag it up to the left this is how it looks like..
This scenario is just OK since the picture is really big that's why I need to be able to drag it.. As you can see at the bottom right, now, the panel is to be seen because I dragged the picture too much in the upper left(which is not right)
Now, what I really want is for it to look like this..
When I do drag, like picture #2 I want the edges of the picture to stay right there. The user now, must not be able to drag it further to the upper left since that is the last part of the picture (technically, the user must not be able to see the background which is the panel) same thing applied to the other side.
Hope this is much clearer :)
Upvotes: 1
Views: 145
Reputation: 9991
Try this code:
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseMove
If (e.Button = Windows.Forms.MouseButtons.Left) Then
Dim x As Integer = (Me.pbpos.X + (Control.MousePosition.X - Me.offset.X))
Dim y As Integer = (Me.pbpos.Y + (Control.MousePosition.Y - Me.offset.Y))
x = Math.Min(Math.Max(x, -(Me.PictureBox1.Width - Me.Panel1.Right)), 0)
y = Math.Min(Math.Max(y, -(Me.PictureBox1.Height - Me.Panel1.Bottom)), 0)
Me.PictureBox1.Location = New Point(x, y)
End If
End Sub
Upvotes: 2