Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable the CListCtrl select option

Tags:

c++

mfc

clistctrl

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.

like image 238
jack Avatar asked Oct 26 '25 14:10

jack


1 Answers

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.

like image 50
Roger Rowland Avatar answered Oct 29 '25 05:10

Roger Rowland