tags:

views:

262

answers:

3

I have a CListBox with custom drawing being used, and need to detect mouse-clicks within each item to perform actions.

I can listen for mouse-clicks on the main control and mess about translating coords into the local space of the RECT for the item under the mouse. But is it possible to register message handlers for clicks on individual list items... are there messages for that?

A: 

I'm not certain I understand why you need to have the XY coordinate inside each item of a Clistbox ?

anyway, AFAIK, Individual items are not CWnd derived objects.

You could get the mouse position inside the control with OnLButtonDown (or up), it returns a CPoint.

After that, use CListBox::GetItemRect to get the rect of the currently selected item, do a bit of pixel computation and you should be able to get the XY inside the rect of the selected item.

Max.

Max
>>I'm not certain I understand why you need to have the XY coordinate inside each item of a Clistbox?Because the custom item allows user interaction; we're simulating what it would be like if MFC supported controls as list items.
John
+1  A: 

You can use the LVM_HITTEST message to find out which item was clicked.

Stefan
LVM messages are for List Views or CListCtrl classes.
Goz
A: 

Well you could just listen for the LBN_SELCHANGE notification. This will fire every time the user clicks a new item. It won't activate if the already selected item is selected though. This may, or may not, be a problem.

Beyond that I'm pretty sure you'll need to intercept WM_LBUTTONUP messages and transform them to the list box's client space ...

OR You could just use a single columned CListCtrl (ListView) class with headers turned off (LVS_NOCOLUMNHEADER). You can then trap the NM_CLICK message. Personally, I massively prefer CListCtrl to CListBox. It IS a little more complex but way more powerful :)

Edit: Or you could try using http://msdn.microsoft.com/en-us/library/bb761323(VS.85).aspx

Goz