Friday, August 8, 2008

CTreeCtrl sample

CBitmap bmpTrreIcons;
bmpTrreIcons.LoadBitmap( IDB_BITMAP1 );
m_ImageList.Create(16, 16, ILC_MASKILC_COLOR16, 0, 1);
m_ImageList.Add( &bmpTrreIcons , RGB(255,255,255));
m_TreeCtrl.SetImageList( &m_ImageList,LVSIL_NORMAL);
HTREEITEM hTree, hSub;
hTree = m_TreeCtrl.InsertItem("About me" , TVI_ROOT );
//m_TreeCtrl.Expand(hTree,TVE_EXPAND);
m_TreeCtrl.SetItemState( hTree, TVIS_EXPANDED TVIS_BOLD ,TVIS_EXPANDED TVIS_BOLD );
hSub = m_TreeCtrl.InsertItem("Personal",0, 1,hTree);
m_TreeCtrl.InsertItem("School",2, 2,hSub);
m_TreeCtrl.InsertItem("College",2, 2,hSub);
m_TreeCtrl.InsertItem("Company",0, 1,hSub);
m_TreeCtrl.Expand(hSub,TVE_EXPAND);


hSub = m_TreeCtrl.InsertItem("Official",0, 1,hTree);
m_TreeCtrl.InsertItem("1st company",2, 2,hSub);
m_TreeCtrl.InsertItem("2nd Company",4, 4,hSub);
m_TreeCtrl.Expand(hSub,TVE_EXPAND);

On Double Click


void CSampleOnDblclkTree1(NMHDR* pNMHDR, LRESULT* pResult)
{
CString csItemText;
CTreeCtrl* pCtrl = (CTreeCtrl*) GetDlgItem(IDC_TREE1);
ASSERT(pCtrl != NULL);
HTREEITEM hCurSel = pCtrl->GetNextItem(TVI_ROOT, TVGN_CARET);
if (hCurSel == NULL)
MessageBox(_T("There is no selected item"),"SampleTool",MB_TOPMOSTMB_APPLMODALMB_OKMB_ICONINFORMATION);
else
{
csItemText = pCtrl->GetItemText(hCurSel);
}
*pResult = 0;
}

No comments: