Question

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)
enter image description here

Now if I drag it up to the left this is how it looks like..
enter image description here
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..
enter image description here
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 :)

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top