区分用户单击和CListCtrl中的SetChecked() [英] Differentiate between user click and SetChecked() in CListCtrl

查看:147
本文介绍了区分用户单击和CListCtrl中的SetChecked()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CListCtrl,其中包含一些复选框,我需要根据某些外部因素启用或禁用这些复选框.但是,当列表中有更多项目可以显示时,我将无法在控件上使用EnableWindow(FALSE),因为它还会禁用滚动条.

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);
            }
        }
    }
}

问题在于,当用户单击复选框(好!)时,以及从代码中调用SetChecked()函数时,也会调用此函数.

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.

我希望对hitInfo.flags进行检查可以使我将点击和功能区分开,但这不是事实.

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.

除了在函数调用之前/之后设置一些全局标志并在回调中使用它之外,还有其他方法可以确定是否使用click或函数调用吗?

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?

推荐答案

我使用相同的inmy程序,并且使用了标志.

I use the same inmy program and I used a flag.

但是我使用LVN_ITEMCHANGING.有了此消息,我可以防止任何更改.

But I use LVN_ITEMCHANGING. With this message I can prevent any change.

我改写了SetCheck(即使它不是虚拟的)并设置了一个标志,然后才更改列表框项目的状态.内部的OnItemChanging例程可以看到标志集并允许更改.返回后直接清除该标志.

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...

这篇关于区分用户单击和CListCtrl中的SetChecked()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆