Some API functions use structures for their arguments. The MousePos application you’ll find it in the MousePos folder on the CD) demonstrates how to declare and use a simple structure. It uses the GetCursorPos() function to getthe current mouse position when the mouse is clicked. This function must report back to the application two values: the X and Y coordinates of the mouse. These values are stored in a structure called POINTAPI, which has two members, X and Y.You can access these values from within your code with the expressions PointAPI. X and PointAPI.Y.
To design the MousePos project follow these steps:
- Open the API Viewer (choose Add-Ins >API Viewer).
- In the API Viewer window, choose File >Load Text File.
- Select the file Win32api.txt, and in the Available Items list double-click the GetCursorPos entry.
- The following declaration will appear in the Selected Items list:
Declare Function’ GetCursorPos Lib ‘user32’ Alias ‘GetCursorPos’ (lpPoint As POINTAPI) As Long.
The argument required for the function is a data structure named POINTAPI, which holds the coordinatesof a point on the screen. To find the definition of the POINTAPI data structure with the API Viewer incomplete the next steps .
- Select Types in the API Type drop-down list. The Available Items list is populated with, all the data structures Used by the API functions.
- Locate the, POINTAPI data structure and double-click its name. Its definition will appear in the Selected Items list as follows:.
x As long.
y As long
- Click the Copy button to copy this definition (along with the declaration of the GetCursorPosO function) into your application’s Code window.
- Add a new Module to the project and paste the selected declaration there. You’ll paste the following declarations from the API Viewer via the Clipboard
Now you can use the GetCursorPos() function in your code. Suppose you want to know the location of the pointer in the Click event. Visual Basic’s Click event doesn’t report the coordinates of the point where the mouse is clicked, but you can request them, with tile GetCursorPos() function. With the previous declarations in the application’s Module, enter the following code in the Form’s Click event:
Private Sub Form_Click()
Di III Mouseloc As POINTAPI
Dilll retValue As Boolean
retValue = GetCursorPos(MouseLoc)
Debug.Print “X Pos = & MouseLoc.x
Debug.Print ‘Y Pos z = & MouseLoc.y
The GetCursorPos() function returns the pointer’s coordinates in pixels. The values you see in the Immediate window correspond to screen pixels. The origin is (0, 0) and it corresponds to the pixel at the upper-left comer of the screen. If the display is set to a resolution of 800 x 600, the pixel at the lower-right comer of the screen will be 799,599). To actually click that coordinate, you must move the Form to the lowerright comer of the screen and then click a pixel. The GetCursorPos() function reports the mouse coordinates in absolute screen units, but the Form_Click event is triggered only when the mouse is clicked somewhere on the Form ..That’s why you must move the Form to another location on the screen, then click. You’ll see later how you can track the pointer, even outside the Form.