mousedown, mousemove

ICW

Active member
Joined
Mar 27, 2006
Messages
25
Programming Experience
Beginner
Hi,
I am trying to get my image to move when I hold the mouse down and drag it. I have some code which sort of works but the image is very judddery and not at all smooth when being dragged. Can anyone help?
See code below;
-----------------------------
PHP:
Private Sub PicImageSingle_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PicImageSingle.MouseDown
 
Dim MousePoint As New Point(e.X, e.Y)
Dim ImagePoint As New Point(Me.PicImageSingle.Left, Me.PicImageSingle.Top)
MousePoint = Me.PicImageSingle.PointToClient(MousePoint)
ImagePoint = Me.PicImageSingle.PointToClient(ImagePoint)
m_DeltaX = MousePoint.X - ImagePoint.X
m_DeltaY = MousePoint.Y - ImagePoint.Y
End Sub
Private Sub PicImageSingle_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PicImageSingle.MouseMove
 
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim MousePoint As New Point(e.X, e.Y)
Dim ImagePoint As New Point(Me.PicImageSingle.Left, Me.PicImageSingle.Top)
Me.PicImageSingle.Left = MousePoint.X - m_DeltaX
Me.PicImageSingle.Top = MousePoint.Y - m_DeltaY
End If
End Sub
 
Clarify somthing

Correct me if I am wrong you are using picture boxes correct? Because it may be possible to try to use a drag drop event instead. I am not sure if that would work or not in this case but you can try.
 
Juddery.. that some is one pretty shaky effect you have produced there, love it! :D

Regarding the effect you really ask for, there are some things to consider.

First, the PointToScreen and PointToClient methods works relative to the control they are called from, first gets a screen point from a client point, second a client point from a screen point.

Second, the points you handle here are client points, but they are points of different clients, the PictureBox location is client of its container (presumably Form (Me)), the mouse location of PictureBox event is client point of PictureBox.

To compare these points you have to convert them to screen points from the control they belong (different!). Here you find the difference of mouse and Picturebox on mouse down.

When moving you want to get the client point of the PictureBox container to set a new location, so you convert the mouse point from PictureBox to screen, then this screen point to client point of Form. Deduct the difference calculated above and set the new location of the Control.

Confused yet? lol :D :D Here is sample code that works if PictureBox is contained by Form.

VB.NET:
Dim m_DeltaX, m_DeltaY As Integer
 
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles PicImageSingle.MouseDown
  If e.Button = Windows.Forms.MouseButtons.Left Then
    Dim MousePoint As Point = Me.PicImageSingle.PointToScreen(e.Location)
    Dim ImagePoint As Point = Me.PointToScreen(PicImageSingle.Location)
    m_DeltaX = MousePoint.X - ImagePoint.X
    m_DeltaY = MousePoint.Y - ImagePoint.Y
  End If
End Sub
 
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles PicImageSingle.MouseMove
  If e.Button = Windows.Forms.MouseButtons.Left Then
    Dim MousePoint As Point = Me.PicImageSingle.PointToScreen(e.Location)
    MousePoint = Me.PointToClient(MousePoint)
    Me.PicImageSingle.Location = New Point(MousePoint.X - m_DeltaX, MousePoint.Y - m_DeltaY)
  End If
End Sub
 
The code above is good to see how the different points and methods work together, this code work better for general purpose (whatever containing the control to move), see also how the Offset calculations was broken down to actually being the client point itself:
VB.NET:
Dim Offset As Point
 
Private Sub PicImageSingle_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles PicImageSingle.MouseDown
  Offset = e.Location
End Sub
 
Private Sub PicImageSingle_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles PicImageSingle.MouseMove
  If e.Button = Windows.Forms.MouseButtons.Left Then
    Dim MousePoint As Point = sender.Parent.PointToClient(sender.PointToScreen(e.Location))
    sender.Location = New Point(MousePoint.X - Offset.X, MousePoint.Y - Offset.Y)
  End If
End Sub
 
Back
Top