分类: C/C++
2008-08-01 17:09:14
CDialog * m_treePages[2]; CString node_name; BOOL InitMytree();我们还要在CMFC_TreeCRTLDlg类的构造函数中为m_treePages[2]分配空间,
m_treePages[0]=new Cdialog11; m_treePages[1]=new Cdialog211;InitMytree()函数为m_mytree的初始化过程
BOOL CMFC_TreeCRTLDlg::InitMytree() { //节点的图标 int i=0; int i_count=2; //载入图标 HICON icon[4]; icon[0]=AfxGetApp()->LoadIcon (IDI_ICON6); icon[1]=AfxGetApp()->LoadIcon (IDI_ICON7); //创建图像列表控件 CImageList *m_imagelist=new CImageList; m_imagelist->Create(16,16,0,7,7); m_imagelist->SetBkColor (RGB(255,255,255)); for(int n=0;n始初化完成后,我们要添加CTreeCtrl的消息响应事件,这样才能让它按我们的要求起作用。我们打开Class Wizard点选IDC_TREE1添加TVN_SELCHANGED消息,并在消息响应函数中写入代码。Add(icon[n]); //把图标载入图像列表控件 } m_mytree.SetImageList(m_imagelist,TVSIL_NORMAL); //为m_mytree设置一个图像列表,使CtreeCtrl的节点显示不同的图标 m_mytree.SetBkColor(RGB(0,250,255));//设置m_mytree的背景色 //创建节点 //父节点 HTREEITEM root0=m_mytree.InsertItem("Dialog1",0,1,TVI_ROOT,TVI_LAST); HTREEITEM root1=m_mytree.InsertItem("Dialog2",0,1,TVI_ROOT,TVI_LAST); //一层子节点 HTREEITEM sub_son0=m_mytree.InsertItem("Dialog 1-1",0,1,root0,TVI_LAST); HTREEITEM sub_son1=m_mytree.InsertItem("Dialog 2-1",0,1,root1,TVI_LAST); //二层孙子节点 HTREEITEM sub_m_son0=m_mytree.InsertItem("Dialog 2-1-1",0,1,sub_son1,TVI_LAST); //建立节点对应的Dialog m_treePages[0]->Create(IDD_DIALOG11,this); m_treePages[1]->Create(IDD_DIALOG211,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); return true; }
void CMFC_TreeCRTLDlg::OnSelchangedTree1(NMHDR* pNMHDR, LRESULT* pResult) { NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR; // TODO: Add your control notification handler code here UpdateData(true); node_name=m_mytree.GetItemText(pNMTreeView->itemNew.hItem); //在标题栏显示节点信息 SetWindowText(node_name); //切换面板 if(node_name=="Dialog 1-1"){ m_treePages[0]->ShowWindow(SW_SHOW); m_treePages[1]->ShowWindow(SW_HIDE); } else if(node_name=="Dialog 2-1-1"){ m_treePages[0]->ShowWindow(SW_HIDE); m_treePages[1]->ShowWindow(SW_SHOW); } UpdateData(false); *pResult = 0; }最后,我们在 CMFC_TreeCRTLDlg::OnInitDialog()初始化函数里调用InitMytree()函数。 程序运行效果: