Select an item even if click is not on left most column


  1. Add a WM_LBUTTONDOWN handler in the CListCtrl derived class.
  2. Code for OnLButtonDown() is given below.
  3. Before calling HitTest(), the x coordinate is changed to 2. This forces the point being tested to fall on the first column. A value of x below 2 fails (it is presumably occupied by the border ).
  4. Note that the call to the base class OnLButtonDown() precedes the call to SetItemState.
  5. This method fails if the first column is not visible. We can use the function HitTestEx() described in an earlier section to cover for the situation when the first column is not visible.
    void CMyListCtrl::OnLButtonDown(UINT nFlags, CPoint point) 
    {
            CListCtrl::OnLButtonDown(nFlags, point);
    
            int index;
            point.x = 2;
    
            if( ( index = HitTest( point, NULL )) != -1 )
            {
                    SetItemState( index, LVIS_SELECTED | LVIS_FOCUSED , 
                                    LVIS_SELECTED | LVIS_FOCUSED);
            }
    }



Comments

  • Other way to do

    Posted by Legacy on 09/19/2002 12:00am

    Originally posted by: Cristian Georgescu

    You don,t have to derived a new class from CListCtrl. Let's suppose that we have dialog window CMyDlg with a listview control m_list. You can handle the NM_CLICK message.

    BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
    //{{AFX_MSG_MAP
    ON_NOTIFY(NM_CLICK, IDC_LIST2, OnClickList)
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()

    void CMyDlg::OnClickList(NMHDR* pNMHDR, LRESULT* pResult)
    {
    NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
    CPoint pt(pNMListView->ptAction);
    pt.x = 2;
    int index = m_list.HitTest(pt,NULL);
    if (index != -1) m_list.SetItemState(index,LVIS_SELECTED|LVIS_FOCUSED,LVIS_SELECTED|LVIS_FOCUSED);
    *pResult = 0;
    }

    Reply
  • Highlighting even with click to right

    Posted by Legacy on 05/28/2000 12:00am

    Originally posted by: Don Stauffer

    It seems to me (at least according to the testing I've done, which is on a multiselect ListView), you have to hook the LVM_HITTEST message also and change the X coordinate on it. Otherwise it doesn't work, for instance, if you have a group of items selected and you try to click on one of them but at the far right.

    I also feel more comfortable using LVM_GETITEMRECT and averaging the Right and Left members, rather than assuming 2 will alway be a good choice for a replacement X value.

    Reply
  • New for Version 4.71 of the control

    Posted by Legacy on 12/08/1999 12:00am

    Originally posted by: Daniel BERMAN


    Now from version 4.71 and up you can use the NM_CLICK notification.

    From the help file:

    This notification is identical to the standard NM_CLICK notification except that in version 4.71 and later, the list view supplies an NMITEMACTIVATE structure instead of an NMHDR structure for the lParam.

    Version 4.71 recieves the address of an NMITEMACTIVATE structure that contains additional information about this notification message. The iItem, iSubItem, and ptAction members of this structure contain information about the item.

    Best Regards,
    Daniel BERMAN

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds