Moving the mouse cursor

How would I go about moving the mouse cursor without moving the mouse; i.e. When a user click on the command button 2 random numbers are generated, X and Y.  Then the mouse cursor would move to the generated random numbers.  therefore the mouse has not been moved but the cursor moves.  I would appreciate some sample code as I am new and the books I have don't help me do this.
master01Asked:
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.

AristippusCommented:
Add the following Declare statement to the General Declarations section of Form1 (note that this Declare statement should be typed as one single line of code):

Declare Sub SetCursorPos Lib "User" (ByVal X As Integer, ByVal Y As Integer)



Then within your code:

Sub Command1_Click()
    Dim X As Integer, Y As Integer
    SetCursorPos X, Y
End Sub


0
master01Author Commented:
I did exactly what you said, but a compile error meassage appeared and said

Compile error:

Constants, Fixed-Length strings, arrays, user-defined types and Declare statments not allowed as Public members of Object modules.



(General) (Declarations)

Declare Sub SetCursorPos Lib "User" (ByVal X As Integer, ByVal Y As Integer)
---------------------------------------
Private Sub Command1_Click()
    Dim X As Integer, Y As Integer
    SetCursorPos X, Y
End Sub
---------------------------------------
0
setiawanCommented:
Hi Master01,

Try to declare as private

Private Declare Sub SetCursorPos Lib "User" (ByVal X As Integer, ByVal Y As Integer)

Private Sub Command1_Click()
    Dim X As Integer, Y As Integer
    'give X and Y value, before call setcursorpos
    X = 5
    Y = 5
    SetCursorPos X, Y
End Sub


  danny
0
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!

master01Author Commented:
Thanks for the help both of you.  Danny, where you put    Lib "User"   I typed it and it gave an error but when I replaced it with "User32" it worked.  Just turn your comment into an answer and you can have the points.

Thanks Lee
0
nutwissCommented:
Actually, to be strictly correct - (ie, you're using a 32-bit version of Windows) you should use:

Private Declare Sub SetCursorPos Lib "User32" (ByVal X As Long, ByVal Y As Long)
0
setiawanCommented:
Hi master01,

Sorry, I just paste the code from rejected answers :)

You are right, It should be "User32", since you use Visual Basic 32-bit

BTW, Thanks for your points


  danny
0

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
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.