如何在vba编程中实现当鼠标经过Listview控件中的item时,输出当前item的值?
目前只知道此控件有一个属性selecteditem可以输出任意item值,但必须鼠标点击,而不是经过。因为我只想用鼠标经过的方式,快速获得经过项的料号,并将名字为此料号的...
目前只知道此控件有一个属性selecteditem可以输出任意item值,但必须鼠标点击,而不是经过。因为我只想用鼠标经过的方式,快速获得经过项的料号,并将名字为此料号的图片显示出来,最终实现显示不同物件图片的效果。
本人最终通过API函数SendMessage实现了此功能,下面是详细代码
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
Const LVM_FIRST = &H1000&
Const LVM_HITTEST = LVM_FIRST + 18
Private Type POINTAPI
X As Long
Y As Long
End Type
Private Type LVHITTESTINFO
pt As POINTAPI
flags As Long
iItem As Long
iSubItem As Long
End Type
'上面的代码放在所有程序最顶端,不能在sub里面
Private Sub ListView1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As stdole.OLE_XPOS_PIXELS, ByVal Y As stdole.OLE_YPOS_PIXELS)
Dim lvhti As LVHITTESTINFO
Dim lItemIndex As Long
Dim BigPic as string
lvhti.pt.X = X
lvhti.pt.Y = Y
lItemIndex = SendMessage(ListView1.hWnd, LVM_HITTEST, 0, lvhti) + 1
BigPic = ThisWorkbook.Path & "\pictures\" 展开
本人最终通过API函数SendMessage实现了此功能,下面是详细代码
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
Const LVM_FIRST = &H1000&
Const LVM_HITTEST = LVM_FIRST + 18
Private Type POINTAPI
X As Long
Y As Long
End Type
Private Type LVHITTESTINFO
pt As POINTAPI
flags As Long
iItem As Long
iSubItem As Long
End Type
'上面的代码放在所有程序最顶端,不能在sub里面
Private Sub ListView1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As stdole.OLE_XPOS_PIXELS, ByVal Y As stdole.OLE_YPOS_PIXELS)
Dim lvhti As LVHITTESTINFO
Dim lItemIndex As Long
Dim BigPic as string
lvhti.pt.X = X
lvhti.pt.Y = Y
lItemIndex = SendMessage(ListView1.hWnd, LVM_HITTEST, 0, lvhti) + 1
BigPic = ThisWorkbook.Path & "\pictures\" 展开
1个回答
展开全部
可以用mousemove事件,listview控件没用过,如果是listbox控件的话,我可以帮你把代码写出来。
追问
谢谢,你按listbox写一下,我试试能不能通用。
追答
Private Sub ListBox1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
On Error Resume Next
Label1.Caption = ListBox1.List(Y \ ListBox1.Font.Size)
End Sub
这里假设用label1来显示
遗憾的是,如果一个界面显示不完的时候,也就是有滚动条的时候,当你拉动滚动条之后,显示会出错。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询