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

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

Add a WM_LBUTTONDOWN handler in the CListCtrl derived class. Code for OnLButtonDown() is given below. 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 ). Note that the […]

Written By
CodeGuru Staff
CodeGuru Staff
Aug 6, 1998
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More




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

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.