Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 374
  • Last Modified:

Automatic mouse manipulation to specific XY coordinates

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
groone
Asked:
groone
  • 2
  • 2
  • 2
  • +2
1 Solution
 
dirtdartCommented:
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
 
lmarceauCommented:
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
 
ngpuddingCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
lmarceauCommented:
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
 
grooneAuthor Commented:
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
 
mrmickCommented:
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
 
dirtdartCommented:
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
 
mrmickCommented:
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

Hire Technology Freelancers with Gigs

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

  • 2
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now