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);
            }
    }

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read