Dragging pictures in PictureBox in. Net
First put a PictureBox on the form and specify a picture to display
Define a series of variables to handle picture dragging
' Handle picture drag
Private M_leftx as Integer
Private M_lefty as Integer
Dim M_mouseposx as Integer
Dim M_mouseposy as Integer
Dim M_driftx as Integer
Dim M_drifty as Integer
and assigns an initial value, which can be done when the form is initialized
ME.M_LEFTX = me.picturebox1.location.x
Me.m_lefty = ME.PICTUREBOX1.LOCATION.Y
Define events to handle mouse clicks
' When the mouse is pressed, turn the mouse into a hand and record the position of the current mouse
Private Sub Picturebox1_mousedown (ByVal sender as Object, ByVal e as System.Windows.Forms.MouseEventArgs) Handles picture Box1.mousedown
Me.cursor = System.Windows.Forms.Cursors.Hand
M_MOUSEPOSX = e.x
M_mouseposy = E.y
End Sub
Define events to handle mouse lift
' Handles the mouse button to raise the event, according to the mouse when the mouse clicks the position which saves, and the current mouse position, calculates the mouse movement offset, borrows this to call moves the picture the function, moves the picture
Private Sub Picturebox1_mouseup (ByVal sender as Object, ByVal e as System.Windows.Forms.MouseEventArgs) Handles Picturebo X1. MouseUp
M_DRIFTX = m_mouseposx-e.x
M_drifty = M_mouseposy-e.y
M_LEFTX = M_leftx-m_driftx
M_lefty = M_lefty-m_drifty
Picturemove (sender, E)
Me.cursor = System.Windows.Forms.Cursors.Arrow
End Sub
' The position of the picture calculated from the offset, repaint the picture
Private Sub Picturemove (ByVal sender as Object, ByVal e as System.Windows.Forms.MouseEventArgs)
Dim Mybit as New System.Drawing.Bitmap (picturebox1.image)
Dim mypicgrh as System.Drawing.Graphics = Me.PictureBox1.CreateGraphics
Mypicgrh.clear (Me.PictureBox1.BackColor)
Mypicgrh.drawimageunscaled (Mybit, m_leftx-152, M_lefty)
Mybit.dispose ()
Mypicgrh.dispose ()
End Sub