Selection changed event in CListView based SDI application

后端 未结 1 626
清歌不尽
清歌不尽 2021-01-17 07:42

I am developing MFC SDI application. My view is derived from CListView class. I would like to handle the selection changed event for the list control. I\'m not

相关标签:
1条回答
  • 2021-01-17 08:35

    All you have to do is add the following to your message map:

    ON_NOTIFY_REFLECT(LVN_ITEMCHANGED, &OnItemChanged)

    And here is your event handler:

    void CMyListView::OnItemChanged(NMHDR* pNMHDR, LRESULT* pResult) 
    { 
        NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR; 
    
        // Did the item state change?
        if (pNMListView->uChanged & LVIF_STATE)
        {
            // Did the item selection change?
            const bool oldSelState = (pNMListView->uOldState & LVIS_SELECTED) != 0x0;
            const bool newSelState = (pNMListView->uNewState & LVIS_SELECTED) != 0x0;
            const bool selStateChanged = oldSelState != newSelState;
            if(selStateChanged)
            {
                // TODO: handle selection change; use newSelState where appropriate
            }
        }
        *pResult = 0; 
    }
    
    0 讨论(0)
提交回复
热议问题