Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to get the x and y coordinates of the cursor

Posted on 2004-09-06
7
963 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBA filters 2 64
Sub or Function is not defined 6 43
Problem to line 23 53
VB6 - Cannot type when doing an extract from MSHFlexgrid to Excel 6 18
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

808 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