Click to See Complete Forum and Search --> : Draging an image around
Punkcake
Dec 16th, 2002, 01:02 PM
Hey there
I'm looking for code to drag an image around on a form. I tried it with the mousedown event of the image but had no luck with that. It should be like the position-bar of winamp... where i can drag the Position-handler to the desired destination.
Any help would be appreciated very much :)
NoteMe
Dec 16th, 2002, 01:11 PM
Record a X and Y coordinat int the mouse down event...and in the mouse move event update the picturebox left and top property after the diffrence in the X and Y coordinate when you started and what they are now....
cyborg
Dec 16th, 2002, 02:23 PM
Yes...Here's an example i made some time ago:
Start a new project and add a CommandButton
the paste this code into the form:
Option Explicit
Dim FirstX As Single
Dim FirstY As Single
Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
FirstX = X
FirstY = Y
End Sub
Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then
If Command1.Left < 0 Then Command1.Left = 0
If Command1.Left > Me.ScaleWidth - Command1.Width Then Command1.Left = Me.ScaleWidth - Command1.Width
If Command1.Top < 0 Then Command1.Top = 0
If Command1.Top > Me.ScaleHeight - Command1.Height Then Command1.Top = Me.ScaleHeight - Command1.Height
If Command1.Left > 0 And Command1.Left < Me.ScaleWidth - Command1.Width Then Command1.Left = Command1.Left + (X - FirstX)
If Command1.Top > 0 And Command1.Top < Me.ScaleHeight - Command1.Height Then Command1.Top = Command1.Top + (Y - FirstY)
If Command1.Left <= 0 And X - FirstX >= 0 Then Command1.Left = Command1.Left + (X - FirstX)
If Command1.Left = Me.ScaleWidth - Command1.Width And X - FirstX <= 0 Then Command1.Left = Command1.Left + (X - FirstX)
If Command1.Top = 0 And Y - FirstY >= 0 Then Command1.Top = Command1.Top + (Y - FirstY)
If Command1.Top = Me.ScaleHeight - Command1.Height And Y - FirstY <= 0 Then Command1.Top = Command1.Top + (Y - FirstY)
Command1.Caption = "(" & Command1.Left / Screen.TwipsPerPixelX & "," & Command1.Top / Screen.TwipsPerPixelY & ")"
End If
End Sub
NoteMe
Dec 16th, 2002, 02:40 PM
Yes..that code should do the trick....:D
Punkcake
Dec 16th, 2002, 05:32 PM
thx alot for the help :D figured it out by myself in the meantime...
cyborg
Dec 16th, 2002, 06:55 PM
hehe ok :p
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.