怎样判断鼠标指针是否在某个对象上
你可使用MouseMove事件来判断断鼠标指针在哪个对象上。代码如下:
Option Explicit
Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Command1。 Caption = "鼠标指针在按钮上。"
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Command1。 Caption = ...全部
你可使用MouseMove事件来判断断鼠标指针在哪个对象上。代码如下:
Option Explicit
Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Command1。
Caption = "鼠标指针在按钮上。"
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Command1。
Caption = "鼠标指针不在按钮上。"
End Sub
此外在VB6中用API函数GetCursorPos可获得鼠标的当前位置。用Timer控件捕获鼠标位置变化来实现。代码如下:
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
Dim OldX As Long
Dim OldY As Long
Dim NewX As Long
Dim NewY As Long
Dim UnitValue As Long
Dim UnitName As String
Dim FormatStr As String
Private X0 As Long
Private Y0 As Long
Const FormatStr1 = "000000。
00"
Const FormatStr2 = "0000。0000"
Private Sub Form_Load()
UnitValue = 1440
UnitName = "英寸"
FormatStr = FormatStr1
Timer1。
Enabled = True
GetCursorPos MousePos
OldX = MousePos。X * Screen。TwipsPerPixelX
OldY = MousePos。
Y * Screen。TwipsPerPixelY
End Sub
Private Sub Timer1_Timer()
GetCursorPos MousePos
NewX = MousePos。
X * Screen。TwipsPerPixelX
NewY = MousePos。Y * Screen。TwipsPerPixelY
Text2 = NewX
Text3 = NewY
X0 = NewX - OldX
Y0 = NewY - OldY
If X0 <> 0 Or Y0 <> 0 Then
Text1 = "鼠标滚动"
Text1。
BackColor = vbRed
Else
Text1 = "鼠标静止"
Text1。BackColor = vbGreen
End If
OldX = NewX
OldY = NewY
End Sub。
收起