7/06/2013

[VC++]How can I expand or collapse when click item in tree control?

The simplest way is to set 'single expand' style on tree ctrl. Without 'single expand', if you just use Expand() function, you will not receive notify such as TVN_ITEMEXPANDED, TVN_ITEMEXPANDING.

However, setting 'single expand' style has also a drawback. It is that expanded item once will be collapsed again when it is unselected. This behaviour is different with usual applications' behaviour. Therefore, if remaining unselected item, you should write code like below.

void CExpandTestDlg::OnItemexpandingTree(NMHDR* pNMHDR, LRESULT* pResult)
{
    NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
    // TODO: Add your control notification handler code here

    //Getting a current item
    HTREEITEM hCurItem = m_Tree.GetSelectedItem();

    //Comparing to a received item and then,
    //if the received item is not same,
    //just return for protecting notify TVN_ITEMEXPANDED
    if(hCurItem != pNMTreeView->itemNew.hItem)
    {
        *pResult = 1;
        return;
    }

    *pResult = 0;
}
 

reference from MSDN


No comments:

Post a Comment