我想在树控制mfc中最后检查项目

时间:2017-02-06 08:57:32

标签: mfc

我试过这个

void GetCheckedItems(const CTreeCtrl& tree, CArray<HTREEITEM> *checkedItems, HTREEITEM startItem = NULL)
{
    if (startItem == NULL)
        startItem = tree.GetRootItem();`

    for (HTREEITEM item = startItem; item != NULL; item = tree.GetNextItem(item, TVGN_NEXT))
    {
        // figure out if this item is checked or not
        UINT state = (tree.GetItemState(item, TVIS_STATEIMAGEMASK) >> 12) & 15;

        if (state == 2)
        checkedItems->Add(item);

        // deal with children if present
        HTREEITEM child = tree.GetNextItem(item, TVGN_CHILD);

        if (child != NULL)
        GetCheckedItems(tree, checkedItems, child);
    }
}

现在我有一个数组包含已检查的值,但我怎么知道我上次检查了哪个。

2 个答案:

答案 0 :(得分:0)

只需使用TVN_ITEMCHANGED处理程序。只要项目状态发生变化,就会调用它。

您需要检查已更改的状态标志。您可以检测TVIS_STATEIMAGEMASK的更改。

答案 1 :(得分:0)

CString m;
    HTREEITEM selItem;
    selItem=m_treeRel.GetSelectedItem();
    UINT uFlags = 0;
    CPoint pt(0, 0);
    GetCursorPos(&pt);
    m_treeRel.ScreenToClient(&pt);
    HTREEITEM hItem = m_treeRel.HitTest(pt, &uFlags);
    if(NULL != hItem && (TVHT_ONITEM  & uFlags))
    {
        /*if(selItem == hItem)
        {
            m=_T("Selected Item....");
        }
        else */
        if(!m_treeRel.GetCheck(hItem))
        {
            m=m_treeRel.GetItemText(hItem);
            //m_treeRel.SetCheck(hItem,true);
        }
        else
        {
            //m=_T("ERROR");
            //m_treeRel.SetCheck(hItem,false);
        }
    }
    ShowMessage(m);
    *pResult = 0;

我用它来找出上次检查了哪个项目....!