How to use the TreeView control in Win32, similar to how the file tree appears in Explorer

Source: Internet
Author: User

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

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.