c++mfcclistbox

get the index of the ClistBox item on Mouse over


I have a simple CListBox control. I dynamically add texts in it. Is it possible to get the index of the item over which the mouse is currently hovering on the listbox? Basically, I want to display a tool tip for each item in the listBox based on the mouse position.

Code samples are really appreciated.


Solution

  • The CListBox::ItemFromPoint member returns the item index of the item nearest to a point.

    The point needs to be in client coordinates of the CListBox. If you receive them in screen coordinates (e.g. by calling GetCursorPos), you will have to translate them calling CWnd::ScreenToClient on the CListBox.

    The second parameter (bOutside) tells you, whether the position is over an item or not. The member function returns FALSE or TRUE, respectively.