Picturebox et souris

drenalyn51 Messages postés 3 Date d'inscription vendredi 26 septembre 2008 Statut Membre Dernière intervention 28 septembre 2008 - 28 sept. 2008 à 17:46
68manu Messages postés 43 Date d'inscription samedi 20 septembre 2008 Statut Membre Dernière intervention 1 octobre 2008 - 28 sept. 2008 à 23:17
Bonjours à tous et à toutes!
Je travail avec VB.net 2008
Voilà mon souci :
J'ai un picturebox que je veux déplacer avec la souris dont voici le code :
Private

Sub
PictureBox2_MouseMove(
ByVal
sender
As

Object
,
ByVal
e
As
System.Windows.Forms.MouseEventArgs)
Handles
PictureBox2.MouseMove

If e.Button = MouseButtons.Left
Then
PictureBox2.Location = PictureBox2.PointToScreen(

New Point(e.X - 16, e.Y - 169))

End
If

End
Sub

Mais ce code crée un déplacement pixel par pixel et moi j'ai besoin que ce déplacement se fasse 10 pixels par 10 pixels .
Est-ce que quelqu'un peut m'aider s'il vous plait?
Merci d'avance!

1 réponse

68manu Messages postés 43 Date d'inscription samedi 20 septembre 2008 Statut Membre Dernière intervention 1 octobre 2008
28 sept. 2008 à 23:17
Salut !

Etonnant que ton code fasse deplacer ta picturebox de 1x1...
C'est pas ce que decris msdn sur la notion de pixels.

Inspire toi de cette page d'aide de VS 2008... Made in Microsoft !

URL aide : ms-help://MS.VSCC.v90/MS.msdnexpress.v90.fr/fxref_system.windows.forms/html/0fe5b287-64e2-efff-dcdf-6015fabbd325.htm.
Au cas ou c'est trop ch... a lire sans la couleur

Private picture As Image
Private pictureLocation As Point

PublicSubNew()
   ' Enable drag-and-drop operations.
   Me.AllowDrop = True
EndSub

ProtectedOverridesSub OnPaint(ByVal e As PaintEventArgs)
   MyBase.OnPaint(e)

   ' If there is an image and it has a location,
   ' paint it when the Form is repainted.
   If (Me.picture IsNot Nothing) And _
     Not (Me.pictureLocation.Equals(Point.Empty)) Then
      e.Graphics.DrawImage(Me.picture, Me.pictureLocation)
   End If
EndSub

PrivateSub Form1_DragDrop(ByVal sender As Object, _
  ByVal e As DragEventArgs) HandlesMyBase.DragDrop
   ' Handle FileDrop data.
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
' Assign the file names to a string array, in
      ' case the user has selected multiple files.
      Dim files As String() = CType(e.Data.GetData(DataFormats.FileDrop), String())
      Try
         ' Assign the first image to the 'picture' variable.
         Me.picture = Image.FromFile(files(0))
         ' Set the picture location equal to the drop point.
         Me.pictureLocation = Me.PointToClient(New Point(e.X, e.Y))
      Catch ex As Exception
         MessageBox.Show(ex.Message)
         Return
      EndTry
   End If

   ' Handle Bitmap data.
   If e.Data.GetDataPresent(DataFormats.Bitmap) Then
      Try
         ' Create an Image and assign it to the picture variable.
         Me.picture = CType(e.Data.GetData(DataFormats.Bitmap), Image)
         ' Set the picture location equal to the drop point.
         Me.pictureLocation = Me.PointToClient(New Point(e.X, e.Y))
      Catch ex As Exception
         MessageBox.Show(ex.Message)
         Return
      EndTry
   End If

   ' Force the form to be redrawn with the image.
   Me.Invalidate()
EndSub

PrivateSub Form1_DragEnter(ByVal sender As Object, _
  ByVal e As DragEventArgs) HandlesMyBase.DragEnter
   ' If the data is a file or a bitmap, display the copy cursor.
   If e.Data.GetDataPresent(DataFormats.Bitmap) _
      Or e.Data.GetDataPresent(DataFormats.FileDrop) Then
      e.Effect = DragDropEffects.Copy
   Else
      e.Effect = DragDropEffects.None
   End If
EndSub

En tout cas a priori il faudrait plutot utiliser PointToClient

Bonne chance
Manu
_____________________
No problems, only soluces
0
Rejoignez-nous