How to Get the Listindex of the cursor position(Listbox,FileList)
I am studying the transparent control. The LISTBOX is transparent, and it is difficult to click on the text, so I can find the corresponding LISTBOXA line number by clicking on the form, and simulate to click.
Get the line number of the cursor position, listindex
how to get (line,col) by mouse from treeview,listview,listbox,filelist,datagrid
Code:
Private Sub List1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
'Returns the current line number pointed by the mouse
Dim ListIndexByMouse As Long
'CODE?
End Sub
Like Vsflexgrid.ocx:
Code:
MouseCol returns the current column number pointed by the mouse
MouseRow returns the current row number pointed to by the mouse
Last edited by xiaoyao; Apr 9th, 2021 at 11:46 PM.
Re: How to Get the Listindex of the cursor position(Listbox,FileList)
Code:
Private Declare Function SendMessageLong Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const LB_GETITEMHEIGHT = &H1A1
Private Sub List1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Caption = GetListIndexMouse(List1, Y)
End Sub
Private Function GetListIndexMouse(LB As ListBox, ByVal Y As Single) As Integer
Dim ih As Long
ih = Me.ScaleY(SendMessageLong(LB.hwnd, LB_GETITEMHEIGHT, 0&, 0&), vbPixels, Me.ScaleMode)
GetListIndexMouse = Int(Y / ih) + LB.TopIndex
End Function
Re: How to Get the Listindex of the cursor position(Listbox,FileList)
Originally Posted by Eduardo-
Code:
***
LB_GETITEMHEIGHT
IT'S VERY GOOD ,THANK YOU (support listbox,filelistbox)
Code:
Option Explicit
'use WS_EX_LAYERED,Need Project1.exe.manifest
'Support Win8 Up
Private Declare Function SendMessageLong Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const LB_GETITEMHEIGHT = &H1A1
Dim MouseListIndex As Long
Private Declare Function ReleaseCapture Lib "user32" () As Long
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_NCLBUTTONDOWN = &HA1
Private Const HTCAPTION = 2
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hwnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long
Private Const WS_EX_LAYERED = &H80000
Private Const GWL_EXSTYLE = (-20)
Private Const LWA_ALPHA = &H2
Private Const LWA_COLORKEY = &H1
Private Declare Function SetFocusAPI& Lib "user32" Alias "SetFocus" (ByVal hwnd As Long)
Private Sub Form_Load()
Me.Picture = LoadPicture("123.jpg")
Dim i As Long
For i = 1 To 100
List1.AddItem "list " & i & "--" & Now
Next
End Sub
Private Sub Command1_Click()
List1.BackColor = vbBlue
SetAlphaColor List1.hwnd, List1.BackColor
List1.Refresh
End Sub
Function SetAlphaColor(hwnd As Long, Optional AlphaColor As Long = vbBlue) As Long
Dim rtn As Long
rtn = GetWindowLong(hwnd, GWL_EXSTYLE)
rtn = rtn Or WS_EX_LAYERED
SetWindowLong hwnd, GWL_EXSTYLE, rtn
SetLayeredWindowAttributes hwnd, AlphaColor, 0, LWA_COLORKEY
SetLayeredWindowAttributes hwnd, AlphaColor, 100, LWA_COLORKEY
SetAlphaColor = rtn
End Function
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then
If X >= List1.Left And X <= List1.Left + List1.Width Then
List1_MouseMove Button, Shift, X, Y - List1.Top
List1.ListIndex = MouseListIndex
'SetFocusAPI& List1.hwnd
List1.SetFocus
End If
End If
End Sub
Private Sub List1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
MouseListIndex = GetListIndexMouse(List1, Y)
Caption = "MouseListIndex=" & MouseListIndex
End Sub
Private Function GetListIndexMouse(LB As ListBox, ByVal Y As Single) As Integer
Dim ih As Long
ih = Me.ScaleY(SendMessageLong(LB.hwnd, LB_GETITEMHEIGHT, 0&, 0&), vbPixels, Me.ScaleMode)
GetListIndexMouse = Int(Y / ih) + LB.TopIndex
End Function
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
List1_MouseMove Button, Shift, X, Y - List1.Top
End Sub
Last edited by xiaoyao; Apr 10th, 2021 at 12:01 AM.