Solved

How to get the x and y coordinates of the cursor

Posted on 2004-09-06
7
947 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
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.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

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