How to disable the CListCtrl select option

雨燕双飞 提交于 2019-11-28 05:32:31

问题


I don't know how to disable the CListCtrl select option. I want to override the CListCtrl class method or handle any window command ? Thanks.


回答1:


If you want to stop the user selecting an item in a CListCtrl, you need to derive your own class from CListCtrl and add a message handler for the LVN_ITEMCHANGING notification.

So, an example class CMyListCtrl would have a header file:

MyListCtrl.h

#pragma once

class CMyListCtrl : public CListCtrl
{
    DECLARE_DYNAMIC(CMyListCtrl)

protected:
    DECLARE_MESSAGE_MAP()

public:
    // LVN_ITEMCHANGING notification handler
    afx_msg void OnLvnItemchanging(NMHDR *pNMHDR, LRESULT *pResult);
};

And then MyListCtrl.cpp:

#include "MyListCtrl.h"

IMPLEMENT_DYNAMIC(CMyListCtrl, CListCtrl)

BEGIN_MESSAGE_MAP(CMyListCtrl, CListCtrl)
    ON_NOTIFY_REFLECT(LVN_ITEMCHANGING, &CMyListCtrl::OnLvnItemchanging)
END_MESSAGE_MAP()

void CMyListCtrl::OnLvnItemchanging(NMHDR *pNMHDR, LRESULT *pResult)
{
    // LVN_ITEMCHANGING notification handler
    LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);

    // is the user selecting an item?
    if ((pNMLV->uChanged & LVIF_STATE) && (pNMLV->uNewState & LVNI_SELECTED))
    {
        // yes - never allow a selected item
        *pResult = 1;
    }
    else
    {
        // no - allow any other change
        *pResult = 0;
    }
}

So you can, for example, add a normal CListCtrl to a dialog, then create a member variable for it (by default it will be CListCtrl) then edit your dialog's header file to #include "MyListCtrl.h and change the list control member variable from CListCtrl to CMyListCtrl.



来源:https://stackoverflow.com/questions/16581153/how-to-disable-the-clistctrl-select-option

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!