listview控件,在执行itemclick事件后弹出窗口,但是关闭窗口之后,控件中的项目随着鼠标的移动而移动?

2024-12-16 00:30:06
推荐回答(1个)
回答1:

向Listview发送一个鼠标左键弹起的消息就可以了:

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 Const WM_LBUTTONUP = &H202

Private Sub Form_Load()
With ListView1
.LabelEdit = lvwManual
.ListItems.Add , , "aaa"
.ListItems.Add , , "bbb"
End With
End Sub

Private Sub ListView1_ItemClick(ByVal Item As MSComctlLib.ListItem)
If Item.Text = "aaa" Then
Form2.Show vbModal '如果form2以非模式方式显示,不会出现楼主所说的问题
'如果form2是以模式方式显示,则在鼠标左键按下时激发ItemClick事件,但Form2显示出来,Listview不会再接收鼠标左键弹起的消息,所以当form2关闭后,ListView还会“以为”左键处于按下状态,所以移动鼠标时项目就会跟着鼠标移动
'这里强制向Listview发送一个鼠标弹起的消息解除拖动状态
SendMessage ListView1.hwnd, WM_LBUTTONUP, 0, 0
End If
End Sub