当前位置:首页 » 程序代码 » 正文

VB编程:判断鼠标位置 (坐标,是否在窗体外)

VB程序要获取鼠标坐标位置,如果只是判断鼠标在窗体内的位置就好办了,只要用窗体的移动事件就行了,代码如下:
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Me.Cls
Print "X="; X
Print "Y="; Y
End Sub

但是要获取窗体外的鼠标位置,VB6.0本身做不了,就只能靠API来做了。先在窗体上画一个计时器,然后在窗体模块输入API函数以及变量定义:
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Type POINTAPI
X As Long
Y As Long
End Type
Dim x1, y1, x2, y2

窗体load事件代码:
Private Sub Form_Load()
x1 = ScaleX(Me.Left, vbTwips, vbPixels)
y1 = ScaleY(Me.Top, vbTwips, vbPixels)
x2 = ScaleX(Me.Width, vbTwips, vbPixels)
y2 = ScaleY(Me.Height, vbTwips, vbPixels)

Timer1.Enabled = True
Timer1.Interval = 7
End Sub

计时器代码:
Private Sub Timer1_Timer()
Me.Cls
Dim lpPoint As POINTAPI
GetCursorPos lpPoint
Print "X="; lpPoint.X
Print "Y="; lpPoint.Y
If lpPoint.X > x1 And lpPoint.X < x1 + x2 And lpPoint.Y > y1 And lpPoint.Y < y1 + y2 Then
Print "鼠标在窗体内"
Else
Print "鼠标在窗体外"
End If
End Sub

打赏 支付宝打赏 微信打赏

来源:济亨网

本文链接:https://wb98.com/post/43.html

VB代码  API  
    << 上一篇 下一篇 >>

    湘公网安备 43011102000514号 - 湘ICP备08100508号