VBGood网站全文搜索 Google

首页 - 经验之谈 - 如何获得鼠标指正的X,Y坐标
发表评论(0)作者:不详, 平台:VB6.0+Win98, 阅读:11716, 日期:2001-04-19
Finding the X and Y co-ordinates of the mouse pointer

In many drawing applications, there is a small area that displays the current cursor position on the screen. This is very simple to do and only requires one Windows API call. The example below brings back the x and y co-ordinates of the current cursor position.


1. Start a new standard EXE project in Visual Basic; form1 is created by default.

2. Add a standard module to the project by clicking on project on the Visual Basic menu and select 注释:Add Module注释:. Module1 is opened up by default.

3. Type the following, making sure that the API call is on one line

Option Explicit

Type POINTAPI 注释:Declare types
    x As Long
    y As Long
End Type

Declare Function GetCursorPos Lib "user32" _
(lpPoint As POINTAPI) As Long 注释:Declare API4. Open up Form1 and add two labels and one timer. Set the interval property of the timer to 1. Open up the code window to form1 and type the following

Option Explicit
Dim z As POINTAPI 注释:Declare variable

Private Sub Timer1_Timer()
GetCursorPos z 注释:Get Co-ordinets
Label1 = "x: " & z.x 注释:Get x co-ordinets
Label2 = "y: " & z.y 注释:Get y co-ordinets
End Sub5. Run the program, by pressing F5 or choose Start from the Run menu. Move the mouse around and watch the two labels on Form1