CListCtrl left mouse click event handler 
Author Message
 CListCtrl left mouse click event handler
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;

Quote:
}

any help would be appreciated,

thanks

Dave



Wed, 25 Aug 2004 05:29:31 GMT  
 CListCtrl left mouse click event handler
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
    }

Quote:
}

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


Quote:
> 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



Wed, 25 Aug 2004 05:52:47 GMT  
 CListCtrl left mouse click event handler
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


Quote:
> 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



Wed, 25 Aug 2004 05:56:41 GMT  
 CListCtrl left mouse click event handler
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

Quote:
> 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



> > 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



Wed, 25 Aug 2004 06:34:18 GMT  
 CListCtrl left mouse click event handler
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

Quote:
}

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

Marco


Quote:
> 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



Fri, 27 Aug 2004 21:11:51 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. How to add a mouse handler that handles double click events

2. Why Mouse Mouse event with every Click event

3. mouse down event interfearnig with double click event

4. Left Mouse Down not always triggering message handler

5. Popup a menu when mouse was left clicked!

6. Ctrl+Left Button click problem in CListCtrl

7. Detect left vs. right click event

8. CListCtrl Header - click handler not being called?

9. Left mouse button event in CDragListBox

10. How to apply an event handler when click on a static text

11. ImageButton Click Event Handler Not Getting Fired, why not

12. NotifyIcon handler not receiving mouse events

 

 
Powered by phpBB® Forum Software