树型控件(tree control)怎么用?我是初学者,想把如下的内容加到树型控件上:
一个目录为"初中部"下面又有"初一","初二","初三"三个子目录,"初一"目录下
又有"初一(1)班","初一(2)班","初一(3)班",初二、初三也是一样,然后各班
下面是每个以学生名字命名的文件,要求把这些逐级加到树型控件上,最下面的一
级是文件名,怎么做?

解决方案 »

  1.   

    去看看msdn上“CTreeCtrl Class Members”里边对每个成员函数都有详细地说明
    不过是英文的。
      

  2.   

    http://202.112.105.179/vc/contents.asp?item=树控制 里边有很多源码 你可以直接拿来用的
      

  3.   

    HTREEITEM h1=m_tree.InsertItem("初中部");
    HTREEITEM h2=m_tree.InsertItem("初一",h1);
    HTREEITEM h3=m_tree.InsertItem("初一(1)班",h2);
    m_tree.InsertItem("YourName",h3);
      

  4.   

    TVINSERTSTRUCT tvis;

    tvis.hParent = TVI_ROOT;
    tvis.hInsertAfter = TVI_LAST;
    tvis.item.mask = TVIF_TEXT | TVIF_PARAM;
    tvis.item.pszText = "Root";
    tvis.item.lParam = 0;
    HTREEITEM hTreeItemRoot = m_ctlTree.InsertItem(&tvis);

    tvis.hParent = hTreeItemRoot;

    tvis.item.pszText = "000001";
    tvis.item.lParam = 1;
    m_ctlTree.InsertItem(&tvis);

    tvis.item.pszText = "000002";
    tvis.item.lParam = 2;
    m_ctlTree.InsertItem(&tvis);

    tvis.item.pszText = "000003";
    tvis.item.lParam = 3;
    m_ctlTree.InsertItem(&tvis);

    m_ctlTree.SelectItem(hTreeItemRoot);
    m_ctlTree.Expand(hTreeItemRoot, TVE_EXPAND);
      

  5.   

    TV_INSERTSTRUCT TCItem;
    TCItem.hParent=TVI_ROOT;
    TCItem.hInsertAfter=TVI_LAST;
    TCItem.item.mask=TVIF_TEXT;
    TCItem.item.pszText="初中部";
    hCZB=InsertItem(&TCItem);TCItem.hParent=hCZB;
    TCItem.item.pszText="初一";
    hC1=InsertItem(&TCItem);
    TCItem.item.pszText="初二";
    hC2=InsertItem(&TCItem);
    TCItem.item.pszText="初三";
    hC3=InsertItem(&TCItem);TCItem.hParent=hC1;
    TCItem.item.pszText="初一(1)班";
    hC1Class1=InsertItem(&TCItem);CString str;
    str="你的学生资料目录\\*.dat";
    char pszText[255];CFileFind finder;BOOL bWorking = finder.FindFile(str);
    while (bWorking)
    {
    bWorking = finder.FindNextFile(); TCItem.hParent=hC1Class1;
    strcpy(pszText,finder.GetFileTitle());
    TCItem.item.pszText=pszText;
    InsertItem(&TCItem);
    }finder.Close();
      

  6.   

        通过“FILE->NEW->PROJECTS->MFC AppWizard(EXE)”建立名为VCTREE的工程,在建立过程中选择基于对话框(Dialog based)的应用;将对话框中的默认控件删除,并将所有对话框属性中的Language域设置为Chinese(P.R.C.),以使应用程序支持中文;建立两个图标IDI_PM和IDI_CJ,用来表示图标的选中和非选中状态,对于每个图标都应建立32X32和16X16两种大小,以保证程序的需要;在对话框窗口中添加树控制对象(TREE  CONTROL),并设置五个按钮“增加|删除|查看|排序|关闭”,其对应标识分别如下:    控制名称        标题名称                标识符号     树控制                                  IDC_TREECTRL     按钮            增  加                  IDC_ADD                     删  除                  IDC_DEL                     查  看                  IDC_VIEW                     排  序                  IDC_SORT                     关  闭                  IDOK      5、选中树控制控件,选择“VIEW->ClassWizard->Memory Variables。骺刂艻DC_TREECTRL         引入成员变量,其变量类型为:       变量名            种类            变量类型     m_TreeCtrl        Control         CTreeCtrl     同时利用“MESSAGES MAP”为各命令按钮增加控制功能函数。     6、然后在代码文件VCTREEDlg.CPP中分别加入如下控制代码:     (1)在文件开始处增加图像列表定义     CImageList Cil1,Cil2;//大小图标像列表     (2)在初始化文件开始处增加代码     BOOL CVCTREEDlg::OnInitDialog()     {    CDialog::OnInitDialog();     ......//原来其它代码     // TODO: Add extra initialization here     // 此处开始增加代码     CVCTREEApp *pApp=(CVCTREEApp *)AfxGetApp();//创建图象列表     Cil1.Create(16,16,ILC_COLOR,2,2);     Cil1.Add(pApp->LoadIcon(IDI_PM));     Cil1.Add(pApp->LoadIcon(IDI_CJ));     m_TreeCtrl.SetImageList(&Cil1,TVSIL_NORMAL); //设置图象列表     DWORD dwStyles=GetWindowLong(m_TreeCtrl.m_hWnd,GWL_STYLE);//获取树控制原风格     dwStyles|=TVS_EDITLABELS|TVS_HASBUTTONS|TVS_HASLINES|TVS_LINESATROOT;     SetWindowLong(m_TreeCtrl.m_hWnd,GWL_STYLE,dwStyles);//设置风格     char * CJ[4]={"玉溪卷烟厂","云南卷烟厂","沈阳卷烟厂","成都卷烟厂"};//根数据名称     char * PM[4][5]={          {"红梅一","红梅二","红梅三","红梅四","红梅五"},//产品数据项          {"白梅一","白梅二","白梅三","白梅四","白梅五"},          {"绿梅一","绿梅二","绿梅三","绿梅四","绿梅五"},          {"青梅一","青梅二","青梅三","青梅四","青梅五"}};     int i,j;     HTREEITEM hRoot,hCur;//树控制项目句柄     TV_INSERTSTRUCT TCItem;//插入数据项数据结构     TCItem.hParent=TVI_ROOT;//增加根项     TCItem.hInsertAfter=TVI_LAST;//在最后项之后     TCItem.item.mask=TVIF_TEXT|TVIF_PARAM|TVIF_IMAGE|TVIF_SELECTEDIMAGE;//设屏蔽     TCItem.item.pszText="数据选择";     TCItem.item.lParam=0;//序号     TCItem.item.iImage=0;//正常图标     TCItem.item.iSelectedImage=1;//选中时图标     hRoot=m_TreeCtrl.InsertItem(&TCItem);//返回根项句柄     for(i=0;i<4;i++){//增加各厂家          TCItem.hParent=hRoot;          TCItem.item.pszText=CJ[i];          TCItem.item.lParam=(i+1)*10;//子项序号          hCur=m_TreeCtrl.InsertItem(&TCItem);          for(j=0;j<5;j++){//增加各产品               TCItem.hParent=hCur;               TCItem.item.pszText=PM[i][j];               TCItem.item.lParam=(i+1)*10+(j+1);//子项序号               m_TreeCtrl.InsertItem(&TCItem);          }          m_TreeCtrl.Expand(hCur,TVE_EXPAND);//展开树     }     m_TreeCtrl.Expand(hRoot,TVE_EXPAND);//展开上一级树     return TRUE;  // return TRUE  unless you set the focus to a control     }     (3)增加树项功能的实现     在增加树项功能时,除了需要定义和设置插入树项的数据结构之外,还需要注意的是新增树项的名称初始时均为“新增数据”,增加后允许用户给数据项设置自定义名称。在编程时应特别注意m_TreeCtrl.EditLabel(hInsert);后面不能跟任何其它程序命令,否则这条编辑指令无效。     void CVCTREEDlg::OnAdd()     {    //增加子项功能函数     HTREEITEM hSel=m_TreeCtrl.GetSelectedItem();//取得选择项句柄     if(hSel==NULL) return;//无任何选项则返回     static int nAddNo=100;//编号大于100为新增数据     TV_INSERTSTRUCT TCItem;//定义插入项数据结构     TCItem.hParent=hSel;   //设置父项句柄     TCItem.hInsertAfter=TVI_LAST;//在最后增加     TCItem.item.mask=TVIF_TEXT|TVIF_PARAM|TVIF_IMAGE|TVIF_SELECTEDIMAGE;//设屏蔽     TCItem.item.pszText="新增数据";     TCItem.item.lParam=nAddNo++;//索引号增加     TCItem.item.iImage=0;//正常图标     TCItem.item.iSelectedImage=1;//选中时图标     HTREEITEM hInsert=m_TreeCtrl.InsertItem(&TCItem);//增加     m_TreeCtrl.Expand(hSel,TVE_EXPAND);     m_TreeCtrl.EditLabel(hInsert);//修改增加的数据     } 
      

  7.   

    Call this function to insert a new item in a tree view control.Example// Gain a pointer to our tree controlCTreeCtrl* pCtrl = (CTreeCtrl*) GetDlgItem(IDC_TREE1);
    ASSERT(pCtrl != NULL);// Insert a root item using the structure. We must
    // initialize a TVINSERTSTRUCT structure and pass its
    // address to the call. TVINSERTSTRUCT tvInsert;
    tvInsert.hParent = NULL;
    tvInsert.hInsertAfter = NULL;
    tvInsert.item.mask = TVIF_TEXT;
    tvInsert.item.pszText = _T("United States");HTREEITEM hCountry = pCtrl->InsertItem(&tvInsert);// Insert subitems of that root. Pennsylvania is
    // a state in the United States, so its item will be a child
    // of the United States item. We won't set any image or states,
    // so we supply only the TVIF_TEXT mask flag. This
    // override provides nearly complete control over the
    // insertion operation without the tedium of initializing
    // a structure. If you're going to add lots of items
    // to a tree, you might prefer the structure override
    // as it affords you a performance win by allowing you
    // to initialize some fields of the structure only once,
    // outside of your insertion loop.HTREEITEM hPA = pCtrl->InsertItem(TVIF_TEXT,
       _T("Pennsylvania"), 0, 0, 0, 0, 0, hCountry, NULL);// Insert the "Washington" item and assure that it is
    // inserted after the "Pennsylvania" item. This override is 
    // more appropriate for conveniently inserting items with 
    // images.HTREEITEM hWA = pCtrl->InsertItem(_T("Washington"),
       0, 0, hCountry, hPA);// We'll add some cities under each of the states.
    // The override used here is most appropriate
    // for inserting text-only items.pCtrl->InsertItem(_T("Pittsburgh"), hPA, TVI_SORT);
    pCtrl->InsertItem(_T("Harrisburg"), hPA, TVI_SORT);
    pCtrl->InsertItem(_T("Altoona"), hPA, TVI_SORT);pCtrl->InsertItem(_T("Seattle"), hWA, TVI_SORT);
    pCtrl->InsertItem(_T("Kalaloch"), hWA, TVI_SORT);
    pCtrl->InsertItem(_T("Yakima"), hWA, TVI_SORT);