首页 > 解决方案 > 使用 TVN_SELCHANGED 的问题似乎是连续循环的

问题描述

我在无模型弹出对话框树控件中有此事件处理程序:

void CAssignHistoryDlg::OnTvnSelchangedTreeHistory(NMHDR *pNMHDR, LRESULT *pResult)
{
    LPNMTREEVIEW    pNMTreeView = reinterpret_cast<LPNMTREEVIEW>(pNMHDR);

    if (!m_bBuildTreeMode)
    {
        if ((pNMTreeView->itemOld.hItem == nullptr && !m_bFirstSelChangeEvent) ||
            pNMTreeView->itemOld.hItem != nullptr)
        {
            m_bFirstSelChangeEvent = true;
            if (m_treeHistory.GetParentItem(pNMTreeView->itemNew.hItem) == nullptr)
            {
                // We must update the correct combo
                // and associated string (in the SERVMEET_S structure)
                if (m_pCombo != nullptr && m_pStrText != nullptr)
                {
                    CString strExtractedName = ExtractName(pNMTreeView->itemNew.hItem);
                    m_pCombo->SetWindowText(strExtractedName);
                    *m_pStrText = strExtractedName;
                }

                GetParent()->PostMessage(UM_SM_EDITOR_SET_MODIFIED, (WPARAM)TRUE);
            }
        }
    }

    *pResult = 0;
} 

我不明白的是,为什么一旦触发了这个事件,它就会进入一个连续的循环。

你有什么不对劲的地方吗?

标签: visual-c++mfc

解决方案


我不知道为什么该消息似乎在一个连续的循环中进行。也许是因为我插入断点或添加临时弹出消息框进行调试。无论哪种方式,我都进行了所需的细微调整:

void CAssignHistoryDlg::OnTvnSelchangedTreeHistory(NMHDR *pNMHDR, LRESULT *pResult)
{
    LPNMTREEVIEW    pNMTreeView = reinterpret_cast<LPNMTREEVIEW>(pNMHDR);

    if (!m_bBuildTreeMode)
    {
        if ((pNMTreeView->itemOld.hItem == nullptr && !m_bFirstSelChangeEvent) ||
            pNMTreeView->itemOld.hItem != nullptr)
        {
            m_bFirstSelChangeEvent = true;
            if (m_treeHistory.GetParentItem(pNMTreeView->itemNew.hItem) == nullptr)
            {
                // We must update the correct combo
                // and associated string (in the SERVMEET_S structure)
                if (m_pCombo != nullptr && m_pStrText != nullptr)
                {
                    CString strExtractedName = ExtractName(pNMTreeView->itemNew.hItem);
                    m_pCombo->SetWindowText(strExtractedName);

                    // Bug fix - Only set as modified if the name is different
                    if(*m_pStrText != strExtractedName)
                        GetParent()->PostMessage(UM_SM_EDITOR_SET_MODIFIED, (WPARAM)TRUE);

                    *m_pStrText = strExtractedName;
                }
            }
        }
    }

    *pResult = 0;
}

如您所见,我更改了发布UM_SM_EDITOR_SET_MODIFIED消息的方式和位置。这会导致我的应用程序正常工作。以前它总是将其设置为已修改(多次)。因此,即使您刚刚保存了文件,它也会再次被标记为已修改。这个问题不再发生。


推荐阅读