visual-c++ - 使用 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
消息的方式和位置。这会导致我的应用程序正常工作。以前它总是将其设置为已修改(多次)。因此,即使您刚刚保存了文件,它也会再次被标记为已修改。这个问题不再发生。
推荐阅读
- python - 如何在 pandas 中有效地对列和 groupby 进行分类?
- python - 有人如何从 JSON 文件中的嵌套数组创建 python 键:值字典,在 python 中有多行?
- mysql - 根据用户拥有的“机会”数量分配行
- r - R:找到值> 0并检查数据帧中两个连续值的总和?
- eigen - 自定义标量类型的两个动态大小的特征矩阵之间的乘积
- bash - 这个 bash 参数扩展语法是什么意思?
- sql - SQL - 当转换和替换已经使用时,修剪并只保留字符串中的最后一个单词
- bash - 搜索第一个模式并搜索第二个模式,然后在第二个模式上方插入新行
- php - 为什么数组键值设置在数组末尾而不是特定位置?
- ruby-on-rails - 我的 Fail2Ban 过滤器似乎不起作用