Take a look at ListView_GetCheckState: http://msdn.microsoft.com/en-us/library/bb761250(VS.85).aspx
and LVN_ITEMCHANGING: http://msdn.microsoft.com/en-us/library/bb774847(VS.85).aspx
--
Ajay
"Ajay Kalra" wrote:
Hi Ajay,
Thanks for the quick reply.
We get a pointer to an NMLISTVIEW structure in case of both both
LVN_ITEMCHANGING and LVN_ITEMCHANGED event handlers but with this structure
how can we make sure whether items’s checked/unchecked state has been
toggled or something else has changed?
I mean is there any bit in this structure member which is set/unset when the
state gets toggled?
int ns = pNMListView ->uNewState & LVIS_STATEIMAGEMASK;
if ( ( ns & 0x2000 ) != 0 )
// Checkbox set
else if ( ( ns & 0x1000 ) != 0 )
// Checkbox unset
else
// Something else happened
// Check or uncheck item. If more than one item is selected then use the
one from the
// hit test to determine how we are setting the others (I.E., if it is off
we turn the other
// selected ones on).
void CMyDlg::OnNMClickList(NMHDR *pNMHDR, LRESULT *pResult)
{
NMLISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
LVHITTESTINFO hitinfo;
*pResult = 0;
bool bChecked = false;
//Copy click point
hitinfo.pt = pNMListView->ptAction;
//Make the hit test...
int nItem = m_cList.HitTest(&hitinfo);
if(hitinfo.flags != LVHT_ONITEMSTATEICON)
return; // Didn't click on an icon
if(m_cList.GetItemState(nItem,LVIS_SELECTED) != LVIS_SELECTED) {
// They clicked on one that is not selected... just change it
// ... do something here
bChecked = m_cList.GetCheck(nItem);
bChecked = !bChecked;
m_cList.SetCheck(nItem,bChecked);
*pResult = 1;
return;
}
// Get the checked state from the one they clicked on, but change all
the ones that are selected
UINT uSelectedCount = m_cList.GetSelectedCount();
// Update all of the selected items.
if (uSelectedCount > 0) {
nItem = -1;
m_cList.SetRedraw(false);
for (UINT i=0;i < uSelectedCount;i++) {
nItem = m_cList.GetNextItem(nItem, LVNI_SELECTED);
m_cList.SetCheck(nItem,bChecked);
}
*pResult = 1;
m_cList.Invalidate();
m_cList.SetRedraw();
}
}
Tom
"asoni12" <aso...@discussions.microsoft.com> wrote in message
news:C3A5D065-15DA-46BB...@microsoft.com...
int oldState = -1;
int newState = -1;
int changedState= -1;
int ns1 = pNMLV->uOldState & LVIS_STATEIMAGEMASK;
int ns2 = pNMLV->uNewState & LVIS_STATEIMAGEMASK;
if ((ns1 & 0x2000) != 0)// find the previous state
{ //Checkbox set
oldState = 1;
}
else if ((ns1 & 0x1000) != 0)
{
//Checkbox unset
oldState = 0;
}
if (-1 != oldState)// If got the previous state then find the new state
{
if ((ns2 & 0x2000) != 0)
{ //Checkbox set
newState = 1;
}
else if ((ns2 & 0x1000) != 0 )
{
//Checkbox unset
newState = 0;
}
if ( (-1 != newState) && (oldState != newState))
{
changedState = newState - oldState;
if (1 == changedState)
{
AfxMessageBox(_T("Item has been checked"));
}
else if (-1 == changedState)
{
AfxMessageBox(_T("Item has been unchecked"));