CTreeCtrl:如何清除所选项目的焦点

时间:2015-07-02 06:06:28

标签: visual-c++ visual-studio-2013 mfc

我是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;

任何人都可以帮我解决它

1 个答案:

答案 0 :(得分:0)

本守则需要用OnSelChanging& amp; 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