Moving mouse pointer

Can I move the mouse pointer, simulate a mouse click, using VB5?


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

yes you can here are some examples:

Dim PaintNow As Integer      ' Declare variable.
Private Sub Form_MouseDown (Button As Integer, Shift As Integer, X As Single, Y As Single)
      PaintNow = True      ' Brush on.
End Sub

Private Sub Form_MouseUp (Button As Integer, X As Single, Y As Single)
      PaintNow = False      ' Turn off painting.
End Sub

Private Sub Form_MouseMove (Button As Integer, Shift As Integer, X As Single, Y As Single)
      If PaintNow Then
            PSet (X, Y)      ' Draw a point.
      End If
End Sub

Private Sub Form_Load ()
      DrawWidth = 10      ' Use wider brush.
      ForeColor = RGB(0, 0, 255)      ' Set drawing color.
End Sub
Here are how you simulate clicks:

vbLeftButton      1      Left button is pressed.
vbRightButton      2      Right button is pressed.
vbMiddleButton      4      Middle button is pressed.
umm, I hate to have to say this, but none of the information presented in the 'answer' or the additional comment has any connection to the question as asked. The information presented in the comment, when used properly in some mouse related events, will tell you which button has been clicked. They do  not simulate a click, however.
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

I have been thinking of doing the same thing.  Here is what I think but I could be TOTAL wrong.

A mouse event is a hardware interrupt which gets translated to a software event.  So far (not trying hard) I haven't found any software controls in VB to control the hardware mouse event.  

Therefore I will have to ASSUME this is how other program works by manipulating images:

(1) hide the actual mouse
(2) disable all mouse events
(3) use a mouse image(e.g. icon) and move that on the screen and do a click
(4)  make something happen by responding to the fake click (e.g. enable mouse click event, call the mouse click event)

BUT:  This doesn't give the user a mouse click visual feed back on a button.  (I don't know if a mouse down picture will help)

I assume this is how other programs work because when a mouse simulated program runs (such as a tutorial), it doesn't respond to my mouse click or move, and I cannot change the position of the screen, minimize or maximize it.  This means the program/code knows the exact location of its buttons.

NOTE:  So what if the resolution of the screen is different, your button position will change.
Move the mouse:

Declare Function SetCursorPos Lib "user32" Alias "SetCursorPos" (ByVal x As Long, ByVal y As Long) As Long

RetVal = SetCurSorPos(800, 600)

Since the coordinates are in pixels, the above line will set the cursor in the bottom-right corner of a screen set to 600*800.

Click the mouse:

(I've never tried this, but it's supposed to work)

Public Const MOUSEEVENTF_MOVE = &H1 '  mouse move
Public Const MOUSEEVENTF_LEFTDOWN = &H2 '  left button down
Public Const MOUSEEVENTF_LEFTUP = &H4 '  left button up
Public Const MOUSEEVENTF_RIGHTDOWN = &H8 '  right button down
Public Const MOUSEEVENTF_RIGHTUP = &H10 '  right button up
Public Const MOUSEEVENTF_MIDDLEDOWN = &H20 '  middle button down
Public Const MOUSEEVENTF_MIDDLEUP = &H40 '  middle button up
Public Const MOUSEEVENTF_ABSOLUTE = &H8000 '  absolute move

Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)


Hope this helps!

P.S. PLEASE don't accept gilbertkid's answer on accident if it's mine you like! Happens a LOT more than you'd think it would. While I'm not desperate for points, that many points really should not go to a wrong answer.
You really should reject gilbertkid's answer anyway even if you don't like mine, since his is wrong, it doesn't actually move or click the mouse.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
leesss, you are somewhat right for those mouse-simulated tutorials, because some don't hide the mouse and few if any actually disable it (the min/max/close buttons on some are just pictures and not actual buttons). It is entirely possible to control the mouse as seen by my above code.

My guess is that windows handles the hardware interrupt to control the "image" of the mouse and this code just gives windows a different set of instructions to follow.

I've created some programs that have the min/max/close buttons as pictures.  Someone told me I can use WM_Close event to control what to do if the user clicks the close box.

When the time comes I will try your suggestion.  If it works it will save me from what I was trying to do.  Thankx.
I wouldn't suggest using the WM_Close event, because however you are doing it, it is bound to be a LOT harder than vb's own built in system. Use the queryunload event. One of the parameters passed to the event is what is causing the form's unload. Without looking it up, I believe that some of the possibilites are: unload command in code, task manager (Ctrl-Alt-Del), system menu, close button, Window's closing, and a couple more.
All you have to do to stop the unload is set cancel to true in the event. I have used it to prevent a Setup app from shutting down Window's before I am ready.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.