选择在基于CListView的SDI应用程序中更改了事件

时间:2016-10-20 09:04:10

标签: c++ mfc

我正在开发MFC SDI应用程序。我的观点来自CListView类。我想处理列表控件的选择更改事件。我无法添加WM_NOTIFY消息处理程序,因为我不知道如何获取创建的列表视图的ID。请帮帮我。

1 个答案:

答案 0 :(得分:1)

您只需将以下内容添加到消息地图中:

ON_NOTIFY_REFLECT(LVN_ITEMCHANGED, &OnItemChanged)

这是你的事件处理程序:

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