如何在LISTBOX中移动鼠标就可以起到CLICK的作用
曾经看到有些软件能够实现这个效果,无非是向LISTBOX发送消息,获得光标所在位置的ITEM;或者干脆模拟一个鼠标左键的单击事件。下面用两种方法分别一试:
方法1 Sendmessage 向LISTBOX发送消息
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 LB_ITEMFROMPOINT = &H1A9
Private Sub Form_Load()
Dim i As Byte
For i = 1 To 20
List1.AddItem String(i, Chr(i + 64))
Next
End Sub
Private Sub List1_Click() '随时看到效果
Me.Caption = "The listitem you select is """ & List1.Text & """"
End Sub
Private Sub List1_Mousemove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim lX As Long, lY As Long, lIndex As Long
lX = CLng(X / Screen.TwipsPerPixelX)
lY = CLng(Y / Screen.TwipsPerPixelY)
lIndex = SendMessage(List1.hwnd, LB_ITEMFROMPOINT, 0, ByVal ((lY * &H10000) + lX))
If lIndex >= 0 And lIndex <= List1.ListCount Then List1.Selected(lIndex) = True
End Sub
方法2 模拟鼠标左键单击
Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Private Const MOUSEEVENTF_LEFTDOWN = &H2
Private Const MOUSEEVENTF_LEFTUP = &H4
Private Const MOUSEEVENTF_ABSOLUTE = &H8000
Private Sub Form_Load()
Dim i As Byte
For i = 1 To 20
List1.AddItem String(i, Chr(i + 64))
Next
End Sub
Private Sub List1_Click() '随时看到效果
Me.Caption = "The listitem you select is """ & List1.Text & """"
End Sub
Private Sub List1_Mousemove(Button As Integer, Shift As Integer, X As Single, Y As Single)
mouse_event MOUSEEVENTF_ABSOLUTE Or MOUSEEVENTF_LEFTDOWN, X, Y, 0, 0
mouse_event MOUSEEVENTF_ABSOLUTE Or MOUSEEVENTF_LEFTUP, X, Y, 0, 0
End Sub
这样,在LISTBOX中移动鼠标时,就会看到窗体的标题动态的变化,当然,你可以把该事件写到MOUSEDOWN 或MOUSEUP 中,达到别的效果。