Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted

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

 

     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

Live as if you were to die tomorrow. Learn as if you were to live forever.
Gandhi
Posted

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.

 

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

 

     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

  • Leaders
Posted

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.

   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.

[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...