使用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;
} 

我不明白的是,为什么一旦触发此事件,它就会连续发生。

有什么事冒犯你吗?

解决方法

我不知道为什么消息似乎是连续不断的。可能是因为我要插入断点或添加临时弹出消息框进行调试。无论哪种方式,我都会做出我需要的细微调整:

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消息的方式和位置。这导致我的应用程序正常工作。以前,它总是将其设置为已修改(多次)。因此,即使您刚刚保存了文件,该文件也会被再次标记为已修改。此问题不再发生。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...