MFC ListView Control: How CListCtrl notifies "checkbox status changed"?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1

Thread: MFC ListView Control: How CListCtrl notifies "checkbox status changed"?

Threaded View

  1. #1
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,059

    MFC ListView Control: How CListCtrl notifies "checkbox status changed"?

    Q: How CListCtrl having LVS_REPORT and LVS_EX_CHECKBOXES styles notifies "checkbox status changed" (e.g. when a user has clicked in a checkbox)?



    A: When an item status has been changed the listview control send LVN_ITEMCHANGED notification to its parent window. This includes the change of checkbox state, but unfortunately this is not (yet) documented.
    However, this is possible by testing the bit 12 and 13 from uNewState member of NMLISTVIEW structure sent with LVN_ITEMCHANGED.

    Example
    Code:
    // MyDialog.h
    class CMyDialog : public CDialog
    {
    // ...
       afx_msg void OnLvnItemchangedList(NMHDR *pNMHDR, LRESULT *pResult);
    }
    Code:
    // MyDialog.cpp
    // ...
       ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST1, &CMyDialog::OnLvnItemchangedList)
    END_MESSAGE_MAP()
    // ...
    // ...
    void CMyDialog::OnLvnItemchangedList(NMHDR *pNMHDR, LRESULT *pResult)
    {
       LPNMLISTVIEW pNMLV = reinterpret_cast(pNMHDR);
    
       if(pNMLV->uChanged & LVIF_STATE) // item state has been changed
       {
          switch(pNMLV->uNewState & 0x3000)
          {
          case 0x2000: // new state: checked
             TRACE1("\n Item %d has been checked", pNMLV->iItem);
             break;
          case 0x1000: // new state: unchecked
             TRACE1("\n Item %d has been unchecked", pNMLV->iItem);
             break;
          }
       }
       *pResult = 0;
    }
    Example
    Code:
    // Similar to previous example but a little bit better written;
    // the "magic" constants are replaced with LVIS_STATEIMAGEMASK and
    // INDEXTOSTATEIMAGEMASK macros defined in commctrl.h.
    // Credits: VictorN
    void C_TestCheckDlg::OnLvnItemchangedList1(NMHDR *pNMHDR, LRESULT *pResult)
    {
        LPNMLISTVIEW pNMLV = reinterpret_cast(pNMHDR);
    
        if(pNMLV->uChanged & LVIF_STATE) // item state has been changed
        {
            switch(pNMLV->uNewState & LVIS_STATEIMAGEMASK)
            {
            case INDEXTOSTATEIMAGEMASK(BST_CHECKED + 1): // new state: checked
                TRACE1("\n Item %d has been checked", pNMLV->iItem);
                break;
            case INDEXTOSTATEIMAGEMASK(BST_UNCHECKED + 1): // new state: unchecked
                TRACE1("\n Item %d has been unchecked", pNMLV->iItem);
                break; 
            }
        }
        *pResult = 0;
    }
    See also How CCheckListBox notifies "checkbox status changed"?
    Last edited by ovidiucucu; April 27th, 2009 at 10:28 AM.
    Ovidiu Cucu
    "When in Rome, do as Romans do."
    Visit: Microsoft Virtual Academy
    Follow: https://twitter.com/#!/ovidiucucu
    My blog: http://codexpert.ro/blog/author/ovidiu-cucu/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center