get the index of the ClistBox item on Mouse over

1.1k Views Asked by At

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.

2

There are 2 best solutions below

5
On BEST ANSWER

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.

0
On

I am writing this answer so that others can get help in case some one else is facing this problem. First things first; you can't directly handle mouse move messages from Clistbox. So, you create a new class based on ClistBox class and then handle OnMouseMove() in that class.Then, inside that OnMouseMove() function you should get the item in the clistbox based on the mouse coordinates (as mentioned by IInspectable).

For code example; see below.

BOOL b;
int n = ItemFromPoint(point,b);
CString str;
GetText( n,str);
AfxMessageBox(str);