The first is the header file, which reads as follows:
#include <tchar.h>#include".. \commonfiles\cmnhdr.h"#include<Windows.h>#include<WindowsX.h>#include<CommCtrl.h>#include"resource.h"#pragmaComment (lib, "Comctl32.lib")BOOL inittreeviewimagelists (HWND hwndtv);//This function is primarily used to add a picture to the TreeView controlhtreeitem Inittreectrl (HWND hwnd,ptstr pszcaption,intNlevel);//This function adds content to the TreeView control
The following are the implementation functions:
#include"InitCtrls.h"externhinstance G_hinst;extern intG_nopen;extern intG_nclose; BOOL inittreeviewimagelists (HWND hwndtv) {himagelist himl; Hbitmap Hbitmap; Hicon Hicon; if(Himl = Imagelist_create ( -, -, False,ilc_color,0))==NULL)returnFALSE; Hbitmap=LoadBitmap (G_hinst,makeintresource (IDB_BITMAP1)); Hicon=LoadIcon (G_hinst,makeintresource (Idi_icon1)); //G_nopen = Imagelist_add (Himl,hbitmap, (HBITMAP) NULL);I started with the method above, but I do not know why not add, return value-1
G_nopen =Imagelist_addicon (Himl,hicon); DeleteObject (HICON); Hbitmap=LoadBitmap (G_hinst,makeintresource (IDB_BITMAP2)); Hicon=LoadIcon (G_hinst,makeintresource (Idi_icon2)); G_nclose=Imagelist_addicon (Himl,hicon); DeleteObject (HICON); if(Imagelist_getimagecount (HIML) <2) returnFALSE; Treeview_setimagelist (GetDlgItem (GetParent (HWNDTV), idc_tree), himl,tvsil_normal); returnTRUE; }htreeitem Inittreectrl (HWND hwnd,ptstr pszcaption,intnlevel) {HWND Hwndtreectrl=GetDlgItem (Hwnd,idc_tree); Tvitem TVI; Tvinsertstruct Tvins; StaticHtreeitem Hprev = (htreeitem) Tvi_first;//Hprev represents the new item currently joined StaticHtreeitem Hprevrootitem = NULL;//Previous level directory StaticHtreeitem Hprevlev2item =NULL; Htreeitem Hti; Tvi.mask= tvif_text| tvif_image| tvif_selectedimage|Tvif_param; Tvi.psztext=pszcaption; Tvi.cchtextmax=sizeof(pszcaption)/sizeof(pszcaption[0]); Tvins.item=TVI; Tvins.hinsertafter=Hprev; if(Nlevel = =1) Tvins.hparent=Tvi_root; Else if(Nlevel = =2) Tvins.hparent=Hprevrootitem; Elsetvins.hparent=Hprevlev2item; Hprev= Treeview_insertitem (hwndtreectrl,&tvins); if(Nlevel = =1) Hprevrootitem=Hprev; ElseHprevlev2item=Hprev; if(Nlevel >1) {Hti=treeview_getparent (Hwndtreectrl,hprev); Tvi.mask= Tvif_image |Tvif_selectedimage; Tvi.hitem=Hti; Tvi.iimage=G_nopen; Tvi.iselectedimage=G_nopen; Treeview_setitem (Hwndtreectrl,&TVI); } returnHprev;}
How to use the TreeView control in Win32, similar to how the file tree appears in Explorer