CTreeCtrl:如何清除所选项目的焦点
Posted
技术标签:
【中文标题】CTreeCtrl:如何清除所选项目的焦点【英文标题】:CTreeCtrl: How to clear the focus of selected item 【发布时间】:2015-07-02 06:06:27 【问题描述】:我是 MFC 新手,我想复制准确的 Ctrl+Page Down 和 Ctrl+Page Up 行为对常规 Page Down/Page Up 键没有任何支持键 (Ctrl/Shift)。我一直在尝试清除在点击 Page Up 和 Page Down 键时自动选择的项目的焦点。
我已尝试使用此代码,但无法正常工作:
case VK_NEXT: // pagedown
case VK_PRIOR: // pageup
lhItem = GetFocusedItem();
if (IsSelected(lhItem))
CTreeCtrl::SetItemState(lhItem, 0, TVIS_SELECTED);
break;
谁能帮我解决这个问题
【问题讨论】:
今后请尽量减少对格式的草率,并阅读Markdown help。 你把“这段代码”放在哪里了? 我将代码保存在 void CiTreeCtrl::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 函数中 【参考方案1】:代码需要写在 OnSelChanging & OnSelChanged 事件处理函数中
void CTreeCtrl::OnSelchanging(NMHDR *pNMHDR, LRESULT *pResult)
HTREEITEM hNew = pNMTreeView->itemNew.hItem;
HTREEITEM hOld = pNMTreeView->itemOld.hItem;
m_bOldItemSelected = hOld && (CTreeCtrl::GetItemState(hOld, UINT(TVIS_SELECTED)) & TVIS_SELECTED);
if (GetSelectedCount() > 1)
if (m_bPgUpState || m_bPgDownState)
//Check the state of New Item
if ((pNMTreeView->itemNew.state & TVIS_SELECTED))
// If the item is selected, so make sure OnSelchanged()
// will "select" it !
m_bNewItemSelected = TRUE;
else if (!(pNMTreeView->itemNew.state & TVIS_SELECTED))
// The New item is not selected, so make sure OnSelchanged()
// will not "re-select" it !
m_bNewItemSelected = FALSE;
CTreeCtrl::SetItemState(hNew, UINT(~TVIS_SELECTED), UINT(TVIS_SELECTED));
void TreeCtrl::OnSelchanged(NMHDR* pNMHDR, LRESULT* pResult)
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
HTREEITEM itemNew = pNMTreeView->itemNew.hItem;
HTREEITEM itemOld = pNMTreeView->itemOld.hItem;
if ((m_bPgUpState || m_bPgDownState) && (GetSelectedCount() > 1)
&& (pNMTreeView->itemOld.hItem != NULL || pNMTreeView->itemNew.hItem != NULL))
// It had the focus so Keep selection at old item
if (itemOld && m_bOldItemSelected)
CTreeCtrl::SetItemState(itemOld, UINT(TVIS_SELECTED), UINT(TVIS_SELECTED));
m_bOldItemSelected = FALSE;
else
// Do-not select the item if it is not selected
CTreeCtrl::SetItemState(itemOld, UINT(~TVIS_SELECTED), UINT(TVIS_SELECTED));
在本文中,您将找到有关 CTreeCtrl 的所有问题的解决方案 Full-Featured Tree Control
【讨论】:
以上是关于CTreeCtrl:如何清除所选项目的焦点的主要内容,如果未能解决你的问题,请参考以下文章
调用 SelectItem 函数时如何强制 CTreeCtrl 不滚动到项目?