半条命所有武器:VB如何实现label 显示鼠标位置坐标?

来源:百度文库 编辑:神马品牌网 时间:2024/04/29 02:21:40
VB如何实现label 显示鼠标位置坐标?

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Label1.Caption = (X)
Label2.Caption = (Y)
End Sub

在界面添加一个label,名为label1,将它的autosize属性设为true;
然后在Form的OnMouseMove事件上写代码

label1.caption = str(x)+","+str(y)

在对象MOUSEMOVE的事件中加入代码,改变label的CAPTION属性。
例:鼠标在表单上移动时,标签label1会显示鼠标位置坐标

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)

Form1.Label1.Caption = "x:" + Str(X) + " y:" + Str(Y)

End Sub

使用WIN API,显示的是鼠标在屏幕的位置
Option Explicit
Private Type PointAPI
X As Long
Y As Long
End Type
Dim MousePos As PointAPI
Private Declare Function GetCursorPos Lib "user32" _
(lpPoint As PointAPI) As Long

Private Sub Form_Load()
Timer1.Enabled = True
Timer1.Interval = 100
GetCursorPos MousePos
End Sub

Private Sub Timer1_Timer()
GetCursorPos MousePos
Label1.Caption = "X:" & MousePos.X * Screen.TwipsPerPixelX & " Y:" & MousePos.Y * Screen.TwipsPerPixelY
End Sub