开发者

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-cli开发者_如何学Gocks 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?


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


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


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜