Solved

Automatic mouse manipulation to specific XY coordinates

Posted on 1997-12-18
8
360 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

786 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