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

2022-01-12 00:00:00 c++ mfc

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

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 able to add WM_NOTIFY message handler as I don't know how to get the ID of the created listview. Please help me.

推荐答案

您所要做的就是将以下内容添加到您的消息映射中:

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

相关文章