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
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…