Solved

How to get the x and y coordinates of the cursor

Posted on 2004-09-06
7
928 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 14

Accepted Solution

by:
aelatik earned 500 total points
Comment Utility
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
Comment Utility
I can email the compiled dll i you like ?
0
 

Author Comment

by:sangack
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now