问题
I have a CListCtrl with checkboxes that I need to enable or disable based on some external factor. However, when I have more items in the list that can be displayed I cannot use EnableWindow(FALSE) on the control as it also disables the scrollbar.
So, I have searched and came up with the following code in the message map:
ON_NOTIFY(LVN_ITEMCHANGED, IDC_CHECKBOX_LIST, OnCheckboxChanged)
The callback function is implemented as:
void CUserPropertiesDialog::OnCheckboxChanged(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*) pNMHDR;
LVHITTESTINFO hitInfo;
hitInfo.pt = pNMListView->ptAction;
int nItem = m_checkBoxList.HitTest(&hitInfo);
if (hitInfo.flags != LVHT_ONITEMSTATEICON) return;
std::string groupName = static_cast<LPCTSTR>(m_checkBoxList.GetItemText(nItem, 0));
if (!CCharmUserAdminGUIApp::getTheCharmUserAdminGUIApp().isAdministrator())
{
if (pNMListView->uChanged & LVIF_STATE)
{
if (((pNMListView->uNewState & INDEXTOSTATEIMAGEMASK(2)) != 0) && ((pNMListView->uOldState & INDEXTOSTATEIMAGEMASK(1)) != 0))
{
CH_INFO1("CUserPropertiesDialog::OnCheckboxChanged - CheckBox Now Selected", groupName);
}
else if (((pNMListView->uNewState & INDEXTOSTATEIMAGEMASK(1)) != 0) && ((pNMListView->uOldState & INDEXTOSTATEIMAGEMASK(2)) != 0))
{
CH_INFO1("CUserPropertiesDialog::OnCheckboxChanged - CheckBox Now Unselected", groupName);
}
}
}
}
The problem is that this function is called when a user clicks the checkbox (good!) but also when the SetChecked()
function is called from code.
I had hoped the check on hitInfo.flags
would enable me to tell the click and the function apart but this is not the case.
Is there, besides setting some global flag before/after the function call and use that in the callback, any other way to tell whether the click or the function call is used?
回答1:
I use the same inmy program and I used a flag.
But I use LVN_ITEMCHANGING. With this message I can prevent any change.
I overwrote SetCheck (even it is not virtual) and set a flag before I Change the Status of a list box item. The internal OnItemChanging Routine sees the flag set and allows the Change. The flag is directly cleared after the return.
So if the same Action is done with the mouse the flag is not set and you Need to check in a different way.
Same when I am loading the box. I set the flag, so that all changes can pass through...
来源:https://stackoverflow.com/questions/28582698/differentiate-between-user-click-and-setchecked-in-clistctrl