///
// vb listBox去右键单击,达到和左键单击效果。方法一:
转自:http://blog.csdn.net/sysdzw/article/details/6105567
有时我们需要在listbox的某个项目上右击然后弹出菜单来操作指定项,可是右击某一条时它并没有呈现选中状态,只有左键单击才可以,这显然不符合我们要求的,不过我们用个api函数稍稍处理下即可,很方便实用。
Option Explicit
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 ' left button down
Private Const MOUSEEVENTF_LEFTUP = &H4 ' left button up
Private Sub Form_Load()
Dim i%
For i = 1 To 5
List1.AddItem i
Next
End Sub
Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 2 Then
mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
End If
End Sub
///
// vb 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 Sub Form_Load()
List1.AddItem "aaa"
List1.AddItem "bbb"
List1.AddItem "ccc"
List1.AddItem "ddd"
List1.AddItem "eee"
End Sub
Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 2 Then
Dim P1 As Long, Pid As Long
P1 = X / Screen.TwipsPerPixelX + Y / Screen.TwipsPerPixelY * 65536
Pid = SendMessage(List1.hwnd, &H1A9, 0, ByVal P1)
If Pid < 65536 Then List1.ListIndex = Pid
End If
End Sub