That was a bit tricky and interesting question, but I managed to get it done.
You can use the following GetLVColIndex function to get the column index of a listview passing the X mouse coordinate as argument.
___
[tt]
Option Explicit
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Declare Function ScreenToClient Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) 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
Dim hti As HD_HITTESTINFO
hti.pt = pt
SendMessage hHdr, HDM_HITTEST, 0, hti
GetLVColIndex = hti.iItem + 1
End Function
Private Sub ListView1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
Debug.Print GetLVColIndex(ListView1, x)
End Sub[/tt]
___
The function GetLVColIndex returns a 1-based index of the column from the x coordinate passed as argument. The return value is 0 if no column lies at the specifed x offset.
Note that this code works only if you have configured your listview to use column headers. If the column headers are not used (i.e. HideColumnHeaders property is True) then the function will fail to report the correct column index.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.