咨询一个关于鼠标位置得问题用VB6.0
在窗体中放上两个Command,一个Label和一个Timer。Command1的Caption为“开始测
定”,Command2的Caption为“退出
写入以下代码:
Private Declare Function GetCursorPos Lib "user32" (lpPoint As pointapi) As
Long
Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long,
ByVal nIndex As Long) As Long
Private Type pointapi '定义GetCu...全部
在窗体中放上两个Command,一个Label和一个Timer。Command1的Caption为“开始测
定”,Command2的Caption为“退出
写入以下代码:
Private Declare Function GetCursorPos Lib "user32" (lpPoint As pointapi) As
Long
Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long,
ByVal nIndex As Long) As Long
Private Type pointapi '定义GetCursorPos中的参数
x As Long
y As Long
End Type
'声名变量:
Dim Z As pointapi
Dim L, S, TOTAL
Public A, B, K, H
'
Private Sub Command1_Click()
GetCursorPos Z '得到初始鼠标位置
A = Z。
x: B = Z。y
Timer1。Enabled = True
End Sub
Private Sub Command2_Click()
End
End Sub
Private Sub Form_Load()
H = GetDeviceCaps&(hdc, 8) '得到屏幕水平分辨率(单位为象素)
K = Screen。
Width '得到屏幕宽度(单位为twip)
Timer1。Interval = 10 '数值约小,测得的结果约精确
Timer1。Enabled = False
S = 0
End Sub
Private Sub Timer1_Timer()
GetCursorPos Z
L = Sqr((Z。
x - A) ^ 2 + (Z。y - B) ^ 2) '每次循环得到鼠标移动距离(单位为象
素),方法有些象微分
S = S + L '得到总的路程(单位为象素)
TOTAL = Int(S * K / H * 2。
54 / 1400 / 100 * 100) / 100 '将S单位由象素划为
米,取到小数点后两位。1缇=2。540/1400/100米。
Label1。Caption = TOTAL
A = Z。
x: B = Z。y '用新位置的坐标替换原来的坐标
End Sub
。收起