Solved

How to get the x and y coordinates of the cursor

Posted on 2004-09-06
7
970 Views
Last Modified: 2012-08-14
I have  VB code which will get me the cursor x and y coordinates. I have used the user32.dll to fetch the coordinates. Here is that code...

*********************************************************
Private Declare Sub GetCursorPos Lib "user32.dll" (lpPoint As POINTAPI)

Private Type POINTAPI
X As Integer
Y As Integer
End Type
Private mPosition As POINTAPI

Call GetCursorPos(mPosition)
msgbox mPosition.X
msgbox mPosition.Y
*********************************************************

Now I need the same code to be executed in a vbs file ie., as a VBScript file. Can anyone tell me how to use windows api in vbs file and give me the vbs code for the above situation.

Question 1. I get the x and y coordinates when I run the code in VB 6.0, but a reference memory error is also poping up and closing VB.

Question 2. I need a vb script code which on executing the vbs file should give me the coordinates.



0
Comment
Question by:sangack
7 Comments
 
LVL 8

Expert Comment

by:mladenovicz
ID: 11988194
It should be sthg like this

  X = window.event.clientX
  Y = window.event.clientY

You should put htis in function and caal it on mousemove event
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 11988198
You can't use API's directly in VBScript, the best you can do is create a DLL that wraps the API call and then use that in your VB file.
0
 

Author Comment

by:sangack
ID: 11988251
Hi Carl_tawn

Can you help me in building that dll? I am not that much expert in VB.

Great that u can paste the dll code or send me to
sanjaykumar.b@gmail.com

Sanjay
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Accepted Solution

by:
aelatik earned 500 total points
ID: 11988265
I don't think you can access the getcursor api in vbscript. You will have to write a DLL to do that.

Create a new active x dll and paste the following code in it : ( Make the projectname : CURSOR and call the class POSITIONS )

Private Type POINTAPI
    X As Long
    Y As Long
End Type

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

Dim MPOS As POINTAPI

Public Property Get X() As Long
    X = MPOS.X
End Property

Public Property Get Y() As Long
    Y = MPOS.Y
End Property

Private Sub Class_Initialize()
    Call GetCursorPos(MPOS)
End Sub

Now compile the DLL and place it in your system32 directory and register it.

You can now access it through vbscript by typing :

Dim POS
Set POS = Wscript.CreateObject("CURSOR.POSITION")
Wscript.echo "Position of X =" & POS.X
Wscript.echo "Position of Y =" & POS.Y
Set POS = Nothing
0
 
LVL 14

Expert Comment

by:aelatik
ID: 11988271
I can email the compiled dll i you like ?
0
 

Author Comment

by:sangack
ID: 11988438
aelatik

You soultion is great. It works fine.
But there is one small modification. Object should be created as ("CURSOR.POSITIONS")

Another Doubt in it. While executing the same it gives the mouse coordinates, but I need the coordinates of the current focus area in the screen.

For Example, in the desktop if a folder is selected ie., highlighted I need the coordinates of the highlighted folder rather than my mouse coordinates.

Hope you can understand.
0
 

Author Comment

by:sangack
ID: 11989183
aelatik

I have posted a Question with title " Is there any API which gives the current focus area's coordinates? "  under "Languages" Topic.

Hope you can help me in that .

Sanjay Kumar
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to make an ADE file by code? 11 99
Recommendation vb6 to vb.net or others 14 207
Help me. 3 70
Problem to With line 4 75
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…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…

733 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