CTreeCtrl的用法

 今天简单的学了下CTreeCtrl,新建一个基于单文档的MFC工程,在resource的IDD_DIALOG1里添加一个tree control,将其styles勾上has buttons,has lines,lines at root,通过classWizard添加一个继承于CDialog的类CExDialog,添加私有成员变量CImageList m_imageList,然后就在OnInitDialog()函数里添加树控件的细节:

BOOL CExDialog::OnInitDialog()
{
    CDialog::OnInitDialog();
    HICON hlcon[
8];
   
    m_imageList.Create(
16, 16, 0, 8, 8 );

    hlcon[
0] = AfxGetApp()->LoadIcon( IDI_ICON1 );
    hlcon[
1] = AfxGetApp()->LoadIcon( IDI_ICON2 );
    hlcon[
2] = AfxGetApp()->LoadIcon( IDI_ICON3 );
    hlcon[
3] = AfxGetApp()->LoadIcon( IDI_ICON4 );
    hlcon[
4] = AfxGetApp()->LoadIcon( IDI_ICON5 );
    hlcon[
5] = AfxGetApp()->LoadIcon( IDI_ICON6 );
    hlcon[
6] = AfxGetApp()->LoadIcon( IDI_ICON7 );
    hlcon[
7] = AfxGetApp()->LoadIcon( IDI_ICON8 );

   
for ( int n =0; n <8; n ++ )
   
{
        m_imageList.Add( hlcon[n] );
    }


    CTreeCtrl
* pTree = (CTreeCtrl*)GetDlgItem( IDC_TREEVIEW1 );
    pTree
->SetImageList( &m_imageList, TVSIL_NORMAL );
    TV_INSERTSTRUCT tvinsert;
    tvinsert.hParent
= NULL;
    tvinsert.hInsertAfter
= TVI_LAST;
    tvinsert.item.mask
= TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_TEXT;
    tvinsert.item.hItem
= NULL;
    tvinsert.item.state
=0;
    tvinsert.item.stateMask
=0;
    tvinsert.item.cchTextMax
=6;
    tvinsert.item.iSelectedImage
=1;
    tvinsert.item.cChildren
=0;
    tvinsert.item.lParam
=0;
    tvinsert.item.iImage
=0;

/**///
    tvinsert.item.pszText ="HOMER";
    HTREEITEM hDad
= pTree->InsertItem( &tvinsert );

/**///
    tvinsert.item.pszText ="Marge";
    HTREEITEM hMom
= pTree->InsertItem( &tvinsert );

    tvinsert.hParent
= hDad;
    tvinsert.item.pszText
="Bart";
    tvinsert.item.iImage
=3;
    pTree
->InsertItem( &tvinsert );

    tvinsert.hParent
= hMom;
    tvinsert.item.pszText
="Lisa";
    tvinsert.item.iImage
=6;
    pTree
->InsertItem( &tvinsert );
   
    tvinsert.item.pszText
="Dilbart";
    HTREEITEM hOther
= pTree->InsertItem( &tvinsert );

    tvinsert.hParent
= hOther;
    tvinsert.item.pszText
="Dogbert";
    tvinsert.item.iImage
=7;
    pTree
->InsertItem( &tvinsert );

    tvinsert.item.pszText
="Ratbert";
    pTree
->InsertItem( &tvinsert );
       
   
return TRUE;  // return TRUE unless you set the focus to a control
                 
// EXCEPTION: OCX Property Pages should return FALSE
}

这样就建立了两个父节点,HOMER和Marge,tvinsert.item.iSelectedImage
设置了当选中这项时显示什么图标,tvinsert.item.iImage设置了当没有选中时显示的图标,tvinsert.item.pszText设置显示出来的文本。如果没有对tvinsert.hParent进行更改设置,那么新添加的节点和上一个节点的父节点为同一节点。
下面这段代码实现了选中某一项进行的更改操作:

void CExDialog::OnSelchangedTreeview1(NMHDR* pNMHDR, LRESULT* pResult)
{
    NM_TREEVIEW
* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
   
    CTreeCtrl
* pTree = (CTreeCtrl*)GetDlgItem(IDC_TREEVIEW1);
    HTREEITEM hSelected
= pNMTreeView->itemNew.hItem;
   
if ( hSelected != NULL )
   
{
       
char text[31];
        TV_ITEM item;
        item.mask
= TVIF_HANDLE | TVIF_TEXT;
        item.hItem
= hSelected;
        item.pszText
= text;
        item.cchTextMax
=30;
        VERIFY( pTree
->GetItem(&item) );
        SetDlgItemText( IDC_TREEVIEW1, text );
    }


   
*pResult =0;
}

有了这些代码还是不够的,如果想显示的话,我们要把这个头文件加入到TreeTestView.cpp里,添加OnLButtonDown事件,并添加一段代码:

CExDialog dlg;     dlg.DoModal();

       ok,这样编译运行下,点击鼠标左键,就会出现一个树状控件,呵呵,简单吧。。。 这些只是实现了静态添加树节点。以后还要深入的学习!

 

 

 

转载于:https://www.cnblogs.com/toconnection/archive/2012/08/08/CTreeCtrl.html

你可能感兴趣的:(CTreeCtrl的用法)