Re: Question about CListCtrl extended style - LVS_EX_CHECKBOXES
This worked for me. Here is what it does, when you select an item that is
not check it will check it. When you unselect an item that is checked it
will uncheck it. when you check an item that is not check it will select it,
and when you uncheck an item that is checked it will also unselect the item.
You have to use Ctrl or Shift keys to multiple select the items.
h file of the CListCtrl class
#pragma once
// CCheckListControl
class CCheckListControl : public CListCtrl
{
DECLARE_DYNAMIC(CCheckListControl)
public:
CCheckListControl();
virtual ~CCheckListControl();
protected:
afx_msg void OnLvnItemchanged(NMHDR *pNMHDR, LRESULT *pResult);
DECLARE_MESSAGE_MAP()
private:
BOOL m_InChange;
};
cpp file of the CListCtrl class
// CheckListControl.cpp : implementation file
//
#include "stdafx.h"
#include "CheckListControl.h"
#include "afxtempl.h"
// CCheckListControl
IMPLEMENT_DYNAMIC(CCheckListControl, CListCtrl)
CCheckListControl::CCheckListControl()
: m_InChange(FALSE)
{
}
CCheckListControl::~CCheckListControl()
{
}
BEGIN_MESSAGE_MAP(CCheckListControl, CListCtrl)
ON_NOTIFY_REFLECT(LVN_ITEMCHANGED, OnLvnItemchanged)
END_MESSAGE_MAP()
// CCheckListControl message handlers
void CCheckListControl::OnLvnItemchanged(NMHDR *pNMHDR, LRESULT *pResult)
{
if (m_InChange)
{
return;
}
m_InChange = TRUE;
LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
// TODO: Add your control notification handler code here
*pResult = 0;
if (((pNMLV->uChanged & LVIF_STATE) == LVIF_STATE) && (pNMLV->uNewState &
LVIS_SELECTED) == LVIS_SELECTED)
{
SetCheck(pNMLV->iItem);
}
else if (((pNMLV->uChanged & LVIF_STATE) == LVIF_STATE) &&
pNMLV->uNewState == 0)
{
if (GetItemState(pNMLV->iItem,LVIS_SELECTED) == 0)
{
SetCheck(pNMLV->iItem,0);
}
}
else
{
SetItemState(pNMLV->iItem,GetCheck(pNMLV->iItem) ? LVIS_SELECTED :
0,LVIS_SELECTED);
}
m_InChange = FALSE;
}
AliR.
"kathy" <yqin_99@yahoo.com> wrote in message
news:1156790194.078868.131070@74g2000cwt.googlegroups.com...
I have set the style:
m_ListCtrl.SetExtendedStyle( LVS_EX_GRIDLINES | LVS_EX_CHECKBOXES );
What I want to get is:
1. when one item is selected, the check box should be checked; If one
item is not selected, it should be unchecked.
2. If item is checked, it should be selected; if not checked, it should
be unselected.
How to do that?