//------------------------------------------
// 初始化樹
//------------------------------------------
// 創建圖像列表控件
CImageList *m_imagelist=new CImageList;
m_imagelist->Create(16,16,0,7,7);
m_imagelist->SetBkColor ( RGB(255,255,255) );
// 把圖標載入圖像列表控件
m_imagelist->Add( AfxGetApp()->LoadIcon(IDI_ICON1) ) ;
m_imagelist->Add( AfxGetApp()->LoadIcon(IDI_ICON2) ) ;
//為 m_mytree 設置一個圖像列表
m_mytree.SetImageList( m_imagelist,TVSIL_NORMAL );
//設置m_mytree的背景色
m_mytree.SetBkColor(RGB(0,250,255));
// 初始化 Tree 節點
HTREEITEM root = m_mytree.InsertItem("TreeTab_demo");
m_mytree.InsertItem( "Dialog1", root );
m_mytree.InsertItem( "Dialog2", root );
m_mytree.Expand( m_mytree.GetRootItem() , TVE_EXPAND);
// 建立節點對應的Dialog
m_treePages[0]->Create(IDD_DIALOG1,this);
m_treePages[1]->Create(IDD_DIALOG2,this);
m_treePages[0]->ShowWindow(SW_SHOW);
m_treePages[1]->ShowWindow(SW_HIDE);
// 把 Dialog 移到合適位置
CRect m_rect;
GetClIEntRect(m_rect);
m_rect.left=200;
m_treePages[0]->MoveWindow(m_rect);
m_treePages[1]->MoveWindow(m_rect);
2. 設置 TVN_SELCHANGED 消息的響 應函數, 在此函數中根據點擊的節點決定顯示的窗口.
LPNMTREEVIEW pNMTreeView = reinterpret_cast<LPNMTREEVIEW>(pNMHDR);
m_strNodeName=m_mytree.GetItemText(pNMTreeVIEw->itemNew.hItem);
//在標題欄顯示節點信息
SetWindowText( m_strNodeName );
//切換面板
if( m_strNodeName =="Dialog1")
{
m_treePages[0]->ShowWindow(SW_SHOW);
m_treePages[1]->ShowWindow(SW_HIDE);
}
else if( m_strNodeName =="Dialog2")
{
m_treePages[0]->ShowWindow(SW_HIDE);
m_treePages[1]->ShowWindow(SW_SHOW);
}