使列表中光标移动到不同的列表项上有不同的提示
Option Explicit
Const LB_ITEMFROMPOINT = &H1A9
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long
Private Sub Form_Load()
Dim i
For i = 1 To 200
List1.AddItem Str(i) + " Samples in this list is " + Str(i)
Next i
End Sub
Private Sub List1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim lXPoint As Long
Dim lYPoint As Long
Dim lIndex As Long
If Button = 0 Then '确定在移动鼠标的同时没有按下功能键或者鼠标键
'获得光标的位置,以像素为单位
lXPoint = CLng(X / Screen.TwipsPerPixelX)
lYPoint = CLng(Y / Screen.TwipsPerPixelY)
'
With List1
'获得 光标所在的标题行的索引
lIndex = SendMessage(.hwnd, LB_ITEMFROMPOINT, 0, _
ByVal ((lYPoint * 65536) + lXPoint))
'将ListBox的Tooltip设置为该标题行的文本
If (lIndex >= 0) And (lIndex <= .ListCount) Then
.ToolTipText = .List(lIndex) 'Return the text = .list(lIndex)
Else
.ToolTipText = ""
End If
End With
End If
End Sub
首先在Form1中加入一个ListBox控件,然后再将上面的代码加入到Form1的代码窗口中。运行程序,当光标在 列表中移动时,可以看到根据光标所在的不同的列表项,提示文字也不相同。
问题是:我把代码复制到Form1窗口后,提示"编译错误",在End Sub、End Function或End属性后面只能出现注释,是哪里错了呢,还望高手们帮帮忙,谢谢。
[解决办法]
我把代码复制到Form1窗口后
是什么意思?
[解决办法]
这段代码没问题,编译通过!
[解决办法]
我试了也行