How to Retrieve Mouse Cursor Coordinates in Visual Basic

Last reviewed: June 21, 1995
Article ID: Q114777
The information in this article applies to:
  • Standard and Professional Editions of Microsoft Visual Basic for Windows, versions 2.0 and 3.0

SUMMARY

Applications such as Paint programs use functions from the Windows API (Application Programming Interface) to retrieve Mouse coordinates that in turn help the user design and paint their picture. This article shows by example how to use the Windows API GetCursorPos() function to retrieve the mouse coordinates from Visual Basic.

MORE INFORMATION

The GetCursorPos() function returns a structure that contains the current position of the caret. To call the GetCursorPos() function from Visual Basic, you have to set up a Type structure. This is shown in the following example.

Step-by-Step Example

  1. Start a new project in Visual Basic. Form1 is created by default.

  2. From the File Menu, choose New Module.

  3. Add the following code in the General Declarations section of the new module:

    Type POINTAPI ' This holds the logical cursor information

          x As Integer
          y As Integer
    
    End Type

       Declare Sub GetCursorPos Lib "User" (lpPoint As POINTAPI)
    
    

  4. Add a timer control (Timer1) to the form.

  5. Add the following code to the Form_load event:

       Sub Form_Load ()
          timer1.Interval = 100
       End Sub
    
    

  6. Add the following code to the Timer1_Timer event:

       Sub Timer1_Timer ()
          Dim rect As POINTAPI
          ' Get the current mouse cursor coordinates:
          Call GetCursorPos(rect)
          FORM1.Cls
          ' Print out current position on the form:
          Print "Current X = " & rect.x
          Print "Current Y = " & rect.y
       End Sub
    
    

  7. Run the Program. You should be able to see the cursor coordinates change whenever you move the mouse around the screen.

REFERENCES

"Programming Windows: the Microsoft Guide to Writing Applications for Windows 3," Charles Petzold, Microsoft Press, 1990

"Microsoft Windows Software Development Kit" Reference Manuals and on-line help

WINSDK.HLP file shipped with Microsoft Windows 3.0 Software Development Kit

"Visual Basic Programmers Guide to the Windows API", Daniel Appleman, Ziff Davis Press, 1993


Additional reference words: 3.00
KBCategory: kbprg kbcode
KBSubcategory: APrgOther


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 21, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.