Tips for applying CTreeCtrl controls in MFC

Source: Internet
Author: User

First, we want to create a basic dialog box for the MFC project MFC_TREECRTL (name casually to one). Then insert two dialog,id in the resource View, idd_dialog11 and idd_dialog211 respectively, and change the style property to the Child,border property to None, and create two classes for them. Named Cdialog11 and Cdialog211, and include the Dialog11.h and Dialog211.h two header files in the Mfc_treecrtldlg.cpp file. Then import several resource icons as icons and decorative panels for the tree control node. Finally, add a CTreeCtrl control on the main panel, the ID is default, and add one of its variables to the ClassWizard, named M_mytree.

Next, we do the specific code writing.

We have to include these variables and functions in the Cmfc_treecrtldlg class.CDialog * m_treePages[2];
  CString node_name;
  BOOL InitMytree();
We also allocate space in the constructor of the Cmfc_treecrtldlg class for m_treepages[2],m_treePages[0]=new Cdialog11;
  m_treePages[1]=new Cdialog211;
The Initmytree () function is the initialization process for the M_mytreeBOOL Cmfc_treecrtldlg::initmytree ()
{
Icon for node
int i=0;
int i_count=2;
Load icon
Hicon Icon[4];
Icon[0]=afxgetapp ()->loadicon (IDI_ICON6);
Icon[1]=afxgetapp ()->loadicon (idi_icon7);
Create an image list control
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<i_count;n++)
{
M_imagelist->add (Icon[n]); Load an icon into the image list control
}
M_mytree. SetImageList (M_imagelist,tvsil_normal); Set an image list for M_mytree so that the CTreeCtrl node displays a different icon
M_mytree. SetBkColor (RGB (0,250,255));//Set background color of M_mytree
Creating nodes
Parent node
Htreeitem Root0=m_mytree. InsertItem ("Dialog1", 0,1,tvi_root,tvi_last);
Htreeitem Root1=m_mytree. InsertItem ("Dialog2", 0,1,tvi_root,tvi_last);
A layer of child nodes
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);
Two-story grandson node
Htreeitem Sub_m_son0=m_mytree. InsertItem ("Dialog 2-1-1", 0,1,sub_son1,tvi_last);
Establish the corresponding dialog of the node
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);
Move the dialog to the right place.
CRect M_rect;
GetClientRect (M_rect);
m_rect.left=200;
M_treepages[0]->movewindow (M_rect);
M_treepages[1]->movewindow (M_rect);
  
return true;
}

Once the initial is complete, we will add the CTreeCtrl message response event to make it work as we requested. We open the Class Wizard Click Idc_tree1 Add tvn_selchanged Message and write code in the message response function.

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;
}

Finally, we call the Initmytree () function in the Cmfc_treecrtldlg::oninitdialog () initialization function. Program Run Effect:

Figure I

By the time we got here, we had pasted a dialog onto the main dialog, allowing different dialog to be pasted alternately on the main dialog by CTreeCtrl the nodes of the control, thus making it easier for us to invoke more functional modules with only a few windows. It doesn't have to be a pop-up window for each module, but it's complicated.

This article supporting source code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.