CTreeCtrl m_wndTree;

创建:
 if (!m_wndTree.Create(WS_CHILD|WS_VISIBLE|TVS_HASLINES|TVS_HASBUTTONS|TVS_LINESATROOT,
CRect(0, 0, 0, 0), &m_wndMyBar1, 100))
{
TRACE0("Failed to create instant bar child\n");
return -1;
}
m_wndTree.ModifyStyleEx(0, WS_EX_CLIENTEDGE); ////往树型控件添加内容///
HTREEITEM hti = m_wndTree.InsertItem(_T("VC知识库在线杂志"));
m_wndTree.InsertItem(_T("电子文档"));
m_wndTree.InsertItem(_T("在线杂志第一期"), hti);
m_wndTree.InsertItem(_T("在线杂志第二期"), hti); //-------------那么如何当点击树形控件中“电子文档”item项时候触发某个事件
如显示相应的文档信息

解决方案 »

  1.   

    写工添加
    //in.h
    afx_msg void OnSelchangedTree(NMHDR* pNMHDR, LRESULT* pResult);
    //in .cpp
    ON_NOTIFY(TVN_SELCHANGED, IDC_TREE, OnSelchangedTree)
    void CTreeViewDlg::OnSelchangedTree(NMHDR* pNMHDR, LRESULT* pResult) 
    {
    m_list.DeleteAllItems();
    NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
    TVITEM item = pNMTreeView->itemNew;
    if(item.hItem == m_hRoot)
    return;
    CString str = GetFullPath(item.hItem);
        if(str.Right(1) != "\\")
       str += "\\";
    str += "*.*";
    CFileFind file;
    BOOL bContinue = file.FindFile(str);
    while(bContinue)
    {
    bContinue = file.FindNextFile();
    if(!file.IsDirectory() && !file.IsDots())
    {
        SHFILEINFO info;
    CString temp = str;
    int index = temp.Find("*.*");
    temp.Delete(index,3);
        SHGetFileInfo(temp + file.GetFileName(),0,&info,sizeof(&info),SHGFI_DISPLAYNAME | SHGFI_ICON);
        int i = m_ImageList.Add(info.hIcon);
        m_list.InsertItem(i,info.szDisplayName,i);
    }
    }
    *pResult = 0;
    }
      

  2.   

    void ListAll::OnSelchangedTree1(NMHDR* pNMHDR, LRESULT* pResult) 
    {
    NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
    // TODO: Add your control notification handler code here
        HTREEITEM hItem = m_TreeCtrl.GetSelectedItem();
    HTREEITEM hTItem = hItem;
    long n=0;
    while(m_TreeCtrl.GetParentItem(hTItem)!=NULL)
    {
    hTItem=m_TreeCtrl.GetParentItem(hTItem);
    n=n++;
    }
        if (hItem != NULL)
    {
        CString strItemText,m_path;
    ParameterStruct a;
    strItemText = m_TreeCtrl.GetItemText(hItem);
    m_Namef = strItemText;
    CString  strItemText1 = m_TreeCtrl.GetItemText(hTItem);
    m_Namet = strItemText1;
        ParameterStruct paraStruct,refparas,temparas,refStruct,rofStruct;
    paraStruct.amount = 0;
    refparas.amount = 0;
    temparas.amount = 0;
    HTREEITEM hParTreeItem = m_TreeCtrl.GetParentItem(hItem);
    CString strParText = m_TreeCtrl.GetItemText(hParTreeItem);
    m_Namep = strParText;
    CCrack crack;
    try
    {
            pDatabaseInter->GetPrjInfor(strItemText, &paraStruct);
        pDatabaseInter->GetPartInfor1(strItemText, &refparas);
    pDatabaseInter->GetRootParts(strItemText, strItemText1, &temparas, n);
          pDatabaseInter->GetRootInfor(strItemText, &rofStruct);
        CString path=crack.strVARIANT(rofStruct.variants[35]);
    if(refparas.amount != 0)
    {
    pDatabaseInter->GetRootPartsC(strItemText, strItemText1, &refStruct, n);
    CString strText = crack.strVARIANT(refStruct.variants[0]);
            m_Page1.PartInforShow(strText, &refparas, m_Namef, m_Namep, m_Namet);
    }
    else
    {
                    m_Page1.PrjInforShow(&paraStruct);
    }
    if(temparas.amount != 0)
    m_Page2.PartInforShow(&temparas);
    else
    m_Page2.PartInforShow(&a);
                if(rofStruct.amount != 0)
    m_Page3.m_view.SetSrc(LPCTSTR(path));
    }
        catch(CInterRecordsetOpenException)
    {
        AfxMessageBox("不能打开数据库结果集,请核对你的SQL语句");
    return;
    }
    catch(CInterException)
    {
    AfxMessageBox("数据库或表格未能打开");
        return;
    }
    }
    *pResult = 0;
    }
      

  3.   

    void ListAll::OnSelchangedTree1(NMHDR* pNMHDR, LRESULT* pResult) 
    {
    NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
    // TODO: Add your control notification handler code here
    //先得到你选择的节点
    //然后写你将进行的操作。
    //比如:弹出对话框
    AfxMessageBox("You OK!");
    *pResult = 0;
    }
      

  4.   

    看一下msdn 中的ON_NOTIFYTo try this example, do the following steps: Create a new dialog box in an existing application. For more information seedialog editor in the Visual C++ User’s Guide.
    You must have an application in which to develop the reusable control. If you don’t have an existing application to use, create a dialog-based application using AppWizard.With your project loaded into Visual C++, use ClassWizard to create a new class called CYellowEdit based on CEdit. Leave the “Add to Component Gallery” box checked.
    Add three member variables to your CYellowEdit class. The first two will be COLORREF variables to hold the text color and the background color. The third will be a CBrush object which will hold the brush for painting the background. The CBrush object allows you to create the brush once, merely referencing it after that, and to destroy the brush automatically when the CYellowEdit control is destroyed.
    Initialize the member variables by writing the constructor as follows:
    CYellowEdit::CYellowEdit()
    {
    m_clrText = RGB( 0, 0, 0 );
    m_clrBkgnd = RGB( 255, 255, 0 );
    m_brBkgnd.CreateSolidBrush( m_clrBkgnd );
    }Using ClassWizard, add a handler for the reflected WM_CTLCOLOR message to your CYellowEdit class. Note that the equal sign in front of the message name in the list of messages you can handle indicates that the message is reflected. This is described inDefining a Message Handler for a Reflected Message in the Visual C++ Programmer's Guide.
    ClassWizard adds the following message-map macro and skeleton function for you:ON_WM_CTLCOLOR_REFLECT()// Note: other code will be in between....HBRUSH CYellowEdit::CtlColor(CDC* pDC, UINT nCtlColor) 
    {
    // TODO: Change any attributes of the DC here// TODO: Return a non-NULL brush if the
    //    parent's handler should not be called
    return NULL;
    }Replace the body of the function with the following code. The code specifies the text color, the text background color, and the background color for rest of the control.
    pDC->SetTextColor( m_clrText );    // text
    pDC->SetBkColor( m_clrBkgnd );    // text bkgnd
    return m_brBkgnd;                // ctl bkgndCreate an edit control in your dialog box, then attach it to a member variable by double-clicking the edit control while holding a control key down. In the Add Member Variable dialog box, finish the variable name and choose “Control” for the category, then “CYellowEdit” for the variable type. Don’t forget to set the tab order in the dialog box. Also, be sure to include the header file for the CYellowEdit control in your dialog box’s header file.
    Build and run your application. The edit control will have a yellow background.
    You can now use Component Gallery to add your CYellowEdit control class to other projects. 
      

  5.   

    //in XXX.h
    afx_msg void OnSelchangedTree(NMHDR* pNMHDR, LRESULT* pResult);
    //in XXX.cpp
    ON_NOTIFY(TVN_SELCHANGED, 100/*树控件ID*/, OnSelchangedTree)
    XXX为处理树控件消息的类
    void CXXX::OnSelchangedTree(NMHDR* pNMHDR, LRESULT* pResult) 
    {
        NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
        *pResult = 0;
    }
    下面的消息也可做类似处理
    NM_CLICK
    TVN_SELCHANGING
    NM_RCLICK
    NM_DBLCLK
    NM_RDBLCLK
      

  6.   

    这样不就是手动添加消息了吗?
    可是
    ON_NOTIFY(TVN_SELCHANGED, IDC_TREE, OnSelchangedTree)
    中IDC_TREE为树形控件的ID,但是再动态创建的过程中没有阿??
    只是
    CTreeCtrl m_wndTree;
    ????
      

  7.   

    sorry知道了
    那么如何让它点击“+”就是父节点前面的加号的时候不响应notify阿??