Computing.Net > Forums > Programming > VB6 - Using the mouse

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

VB6 - Using the mouse

Reply to Message Icon

Original Message
Name: 110359
Date: November 9, 2005 at 16:37:00 Pacific
Subject: VB6 - Using the mouse
OS: WinXP
CPU/Ram: 2.4GHz P4/512MB
Comment:

Hey,

Basically I just need someone to help me with something I think should be fairly simple, but I'm struggling with.

I've got a picture on a form and I want to be able to move the picture around the screen, using the mouse, without having to click a mouse button.

If anybody has any ideas I would greatly appreciate them.


Report Offensive Message For Removal


Response Number 1
Name: StuartS
Date: November 9, 2005 at 17:47:58 Pacific
Reply: (edit)

Assuming you are using a picture box, there is a mouse move event. But that wont do what you want because the event is triggered when the mouse moves over the picture. when this happens the X,Y co-ordinates will hold the current position of the mouse. Move the mouse again and you will triggered the event again and the X,Y co-orientates will then hold the new location of the mouse.

To make the picture move you are going to have to determine where the mouse came from, not where is is now. The move the picture box relative to the difference between where the mouse was to where it is now.

You could store the X,Y coordinates in a public variables, but the results are going to be unpredictable.


Stuart


Report Offensive Follow Up For Removal

Response Number 2
Name: Chi Happens
Date: November 10, 2005 at 03:49:45 Pacific
Reply: (edit)

Use the mouse move event on the form.

' This assumes that you have a picture on your form called Image1
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Image1.Left = X + 2
Image1.Top = Y + 2
End Sub

If you want to drop the image, simply capture the mouseup or mousedown event and then set the Image's left and top to X, Y respectively. In addition, you might want a public boolean that lets the system know if you want to move the image or not, like this:

' This assumes that you have a picture on your form called Image1
Dim Moving As Boolean

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Moving = True
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Image1.Left = X + 2
Image1.Top = Y + 2
End Sub

Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Moving = False
Image1.Left = X
Image1.Top = Y
End Sub

Hope this helps,
Chi

They mostly come at night...mostly.


Report Offensive Follow Up For Removal

Response Number 3
Name: Chi Happens
Date: November 10, 2005 at 03:51:02 Pacific
Reply: (edit)

oops, i forgot to implement moving.


Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Moving Then
Image1.Left = X + 2
Image1.Top = Y + 2
End If
End Sub


Chi

They mostly come at night...mostly.


Report Offensive Follow Up For Removal

Response Number 4
Name: StuartS
Date: November 10, 2005 at 05:04:27 Pacific
Reply: (edit)

But this requires a mouse click and only moves the image in one direction. What happens when the image reaches the extremes of X and Y. How do you get it back again?

Stuart


Report Offensive Follow Up For Removal

Response Number 5
Name: Chi Happens
Date: November 17, 2005 at 06:11:36 Pacific
Reply: (edit)

what do you mean one direction?
it moves the image along with the mouse. Not in only one direction. You can remove the mouse click if you want. this was simply an example showing you how to do it.

Chi

They mostly come at night...mostly.


Report Offensive Follow Up For Removal


Response Number 6
Name: Chi Happens
Date: November 17, 2005 at 06:14:47 Pacific
Reply: (edit)

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Image1.Left = X + 2
Image1.Top = Y + 2
End Sub


Here it is with no click needed.

They mostly come at night...mostly.


Report Offensive Follow Up For Removal






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home








Do you have your own blog?

Yes
No
I did before
I will soon


View Results

Poll Finishes In 4 Days.
Discuss in The Lounge
Poll History




Data Recovery Software