Panning a picturebox control

bjwade62

Centurion
Joined
Oct 31, 2003
Messages
104
I have a picturebox control that I want to create a pan movement that follows the cursor's movement.

I need some help getting started. I've looked at mouse down/up events along with the cursor.location property but can't seem to get it working. Any ideas?

Thanks
 
So I understand, you want to know the postion of the cursor when over the picturebox while not clicking correct? If so check this out. Then hit F1 key to get the help for MouseHover

Visual Basic:
      Private Sub panel1_MouseHover(sender As Object, e As System.EventArgs) Handles panel1.MouseHover
'code here for events you want, as in location of the cursor over the picture box.
End Sub
 
Thanks for the reply. What I'm looking to do is change an image's location base on the cursor's location. Kind of a grap and move thing.

techmanbd said:
So I understand, you want to know the postion of the cursor when over the picturebox while not clicking correct? If so check this out. Then hit F1 key to get the help for MouseHover

Visual Basic:
      Private Sub panel1_MouseHover(sender As Object, e As System.EventArgs) Handles panel1.MouseHover
'code here for events you want, as in location of the cursor over the picture box.
End Sub
 
A real quick and easy way to do this is to place a picture box within a panel. The picture box should size to fit the image. At that point all you have to do is handle mouse events. On MouseDown you want to record the mouse position, and on MouseMove you want to examine the difference between the original position and the new position and move the picture box that distance.

This is the jist of it. You will probably want to take certain things into consideration, such as which buttons are being clicked.
Visual Basic:
    Dim DragX As Integer
    Dim DragY As Integer
 
    'Grab the position on mouse down
    Private Sub imgPicture_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles imgPicture.MouseDown
        DragX = e.X
        DragY = e.Y
    End Sub
 
    ' Move the image based on mouse movements
    Private Sub imgPicture_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles imgPicture.MouseMove
        If (e.Button <> MouseButtons.None) Then
            Dim Location As Point = imgPicture.Location
            Location.X += e.X - DragX
            Location.Y += e.Y - DragY
            imgPicture.Location = Location
        End If
    End Sub
You also might want to see if there is a way to double buffer the picturebox/container for smoother scrolling. (Try subclassing the PictureBox class and using the SetStyle method to set AllDrawingInWmPaint and DoubleBuffering to true in the constructor. Also consider scrapping the PictureBox-in-a-Panel idea and do your own drawing.)

If you can't get it up and running to your satisfaction I'll be glad to offer more assistance.
 
Back
Top