Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

CListCtrl left mouse click event handler

1,074 views
Skip to first unread message

David Reynolds

unread,
Mar 8, 2002, 4:29:31 PM3/8/02
to
Hi I want to be able to tell which item the user clicked. The only problem
I have is that the HitTest always returns -1, when it should return the
index of the item I clicked.

here is the function:

void CPageNotes::OnNMClickListNotes(NMHDR *pNMHDR, LRESULT *pResult)
{
// TODO: Add your control notification handler code here

// get mouse coords
DWORD mousePos = GetMessagePos();

POINT p;
CPoint cp;
p.x = GET_X_LPARAM(mousePos);
p.y = GET_Y_LPARAM(mousePos);
// change mouse coords to CListCtrl mouse coords
ScreenToClient(&p);
cp.x = p.x;
cp.y = p.y;
UINT flag;

// check if click hit a item
if( m_Notes.HitTest(cp,&flag) == -1 )
{
MessageBox("hi","crap",MB_OK);
}
*pResult = 0;
}

any help would be appreciated,

thanks

Dave


Chengrong Lu

unread,
Mar 8, 2002, 4:52:47 PM3/8/02
to
I guess you actually want to find out which SUBItem is clicked,
if your answer is yes, you should do SubItemHitTest() instead of
HitTest().

Followed is a piece of sample code:

CListCtrl& ctrlDataList = GetListCtrl();

C**::OnLClick(NMHDR* pNMHDR, LRESULT* pResult)
{
CListCtrl& ctrlDataList = GetListCtrl();

DWORD dwPos = ::GetMessagePos();
CPoint pointCur((int)LOWORD(dwPos), (int)HIWORD(dwPos));
ctrlDataList.ScreenToClient(&pointCur);

LVHITTESTINFO lvhti;
lvhti.pt = pointCur;
ctrlDataList.SubItemHitTest(&lvhti);
if(lvhti.flags & LVHT_ONITEMLABEL)
{
// lvhti.sSubItem now equals to subitem index
}
}

BTW, HitTest() only works for the first Column.


"David Reynolds" <david_m_...@hotmail.com> wrote in message
news:3c892dd8$1...@corp-news.newsgroups.com...

sszelei

unread,
Mar 8, 2002, 4:56:41 PM3/8/02
to
Dave:

Is CPageNotes the class that is a subclass of the CListControl?
If not then the CPoint you are building is based on the top left of the
container of the List control and therefor Hit Test is reporting correctly.

There are two things you can do here. The best way is to subclass the
CListControl and handle the click event through OnLButtonDown(UINT nFlags,
CPoint point) notice here the point is given to you and you do not have to
generate it. This is the "more correct" :-) way since in OOP each object
should handle it's own events anyway.
The other way is to offset the calculated point by the difference between
the top left of the container and the top left of the List control.

Hope this helps.

Steven Szelei


"David Reynolds" <david_m_...@hotmail.com> wrote in message
news:3c892dd8$1...@corp-news.newsgroups.com...

David Reynolds

unread,
Mar 8, 2002, 5:34:18 PM3/8/02
to
Hi Steven,

CPageNotes is a subclass of CPropertyPage, so I guess I'd have to calculate
the offset. Buy how do I do that?

thanks,

Dave
"sszelei" <ssz...@softprossoftware.com> wrote in message
news:uFfdDwuxBHA.2520@tkmsftngp05...

Marco

unread,
Mar 11, 2002, 8:11:51 AM3/11/02
to
If you are catching the NM_CLICK notification, then you should be able to get
the iItem and iSubItem from pNMHDR


ON_NOTIFY_REFLECT(NM_CLICK, OnClick)

void CBaseListCtrl::OnClick(NMHDR* pNMHDR, LRESULT* )
{
NMITEMACTIVATE * pItem = (NMITEMACTIVATE*)pNMHDR;

pItem->iSubItem; // here is your iSubItem
pItem->iItem; // here is your iItem
}

I have only used this in report mode, but i suppose it should work in other
modes as well.


Marco

"David Reynolds" <david_m_...@hotmail.com> wrote in message
news:3c892dd8$1...@corp-news.newsgroups.com...

0 new messages