Mr. Boy
Mr. Boy

Reputation: 63728

Get relative X/Y of a mouse-click on a MFC CListBox item

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?

Upvotes: 0

Views: 1627

Answers (4)

Software_Designer
Software_Designer

Reputation: 8587

Use the DPtoLP function to convert device coordinates into logical coordinates. http://msdn.microsoft.com/en-us/library/dd162474(v=vs.85).aspx

Upvotes: 0

Goz
Goz

Reputation: 62323

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

Upvotes: 0

Stefan
Stefan

Reputation: 43575

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

Upvotes: 1

Max
Max

Reputation: 3180

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.

Upvotes: 0

Related Questions