Stack Exchange
I want to display item lastly checked in tree control mfc
I tried this 
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);
    }
}

now I have an array contains checked values but how do i know which i checked last time.

See I have one tree control on my dialog and in that i added some nodes and leafs and now  I want to retrieve one specific item which is checked recently

Just use a handler to TVN_ITEMCHANGED. It is called whenever an item state is changing. 
You need to check the state flags that are changed. You can detect changes to the TVIS_STATEIMAGEMASK. 

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;

I Used This to find out which item was checked last time....!
