Solved

Automatic mouse manipulation to specific XY coordinates

Posted on 1997-12-18
8
353 Views
Last Modified: 2008-02-01
In VB5 -- I am working on a project that currently reads the XY coordinates from the windows desktop and displays them in two different labels.  I have two text boxes to retrieve from the user the newly found XY coordinates.  Now when they press 'GO' I want to have the mouse cursor move to the specified coordinates and then do some action, like double clicking. Could you please help me?
0
Comment
Question by:groone
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 5

Expert Comment

by:dirtdart
ID: 1445825
Declare Function SetCursorPos Lib "user32" Alias "SetCursorPos" (ByVal x As Long, ByVal y As Long) As Long


The declare above will set the mouse to the specified position.  If it is performing an action that occurs within your program when it gets there, then the easiest way is to just call the subroutine for that action.  If not, then you'll have to use the following:

Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

where hWnd is the hWnd of the form receiving the message, wMsg is the message you want to send (WM_MBUTTONDOWN, etc), and wParam and lParam can be set to zero and ignored.
0
 
LVL 1

Expert Comment

by:lmarceau
ID: 1445826
The Mouse_Event() API function is able to activate the clickings, the moving, and anything the user can do with the mouse. Much more simple than having to code more lines.

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

As for the correct usage, you've to find out coz' I haven't used them before. Save your work before meddling with APIs.
0
 
LVL 1

Expert Comment

by:ngpudding
ID: 1445827
Okay, I suppose I need someone to hold my hand...but heck for 200 points...any way, Dirts declaration was good. I had just about that same function declaration, and there was an error in lmarceau's declarations - PackedPt = Pt.y * 2^^16 + Pt.x
And thank you ngpudding.

[yes or no question] Is the SetCursorPos used in the same aspect as to what I have below

I think I am confused to how the function and declarations are used in this case.  The code I am using to get the coordinates is as follows:
=======
Option Explicit

Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long

Private Type POINTAPI
        X As Long
        Y As Long
End Type
Private gStop As Boolean
----------------

Private Sub Form_Load()

Dim mousePT As POINTAPI
Dim X As Long
Dim Y As Long

  gStop = False

  Do
    If gStop = True Then Exit Do
    Call GetCursorPos(mousePT)
    X = mousePT.X
    Y = mousePT.Y
    Form2.Label1 = X
    Form2.Label1 = Y
    DoEvents
  Loop
End Sub
-------------
Private Sub form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
  gStop = True
End Sub
======
so in retrospect, the SetCursorPos should be used in the same method where X = Text1 and Y = Text2?  Give me a good answer Dirt and you will get an "A"
0
 
LVL 1

Expert Comment

by:lmarceau
ID: 1445828
Dirdart should get credit for this one...  Use the following for your declare...

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

Create a project, place a single command button on the form and in its click event place the following code

SetCursorPos x, y

This will demonstrate how to use it.

0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 3

Author Comment

by:groone
ID: 1445829
Exactly.  You've got the hang of it.  Just take the values contained in Text1 and Text2 (you might consider changing them to Single with CSng (X = CSng(Text1.Text))) and use SetCursorPos

X = CSng(Text1.Text)
Y = CSng(Text2.Text)

SetCursorPos X, Y

That should do it.
0
 
LVL 8

Expert Comment

by:mrmick
ID: 1445830
Next time, don’t reject an answer when you think it's on track just because you're having trouble... just comment to that affect.
0
 
LVL 5

Accepted Solution

by:
dirtdart earned 200 total points
ID: 1445831
Sorry about rejecting it Dirt, this being my first time around, wasn't entirely sure how to do it.  Thank you all for the information, each of you gave information that will come in useful.  Well, since I am not expert material, I better go buy some more points -- a just in case type of thing you know.

One final question if I may....At this point, I can then use
the
PostMessage hwnd, WM_LBUTTONDOWN, MK_LBUTTON, PackedPt
method as described by lmarceau to cause the click of the mouse..correct?

Again!  Thanks everyone, I finally found a place for some answers!
0
 
LVL 8

Expert Comment

by:mrmick
ID: 1445832
Yes, you can use the PostMessage from lmarceau or the SendMessage that I described to you.  Either one will perform the event you are looking for.  I know this subject in and out, because I once made a Tutorial for Surfing our corporate intranet.  It would automatically move the cursor to the next link (except I did mine incrementally, so it looked like the mouse was actually moving), and automatically click on the link to send the user to the next page of the tutorial.  If you have any questions, email me at dirtdart01@yahoo.com.  I'll do what I can for you.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now