VC controls TreeCtrl and presentation of ListCtrl: Genbutsu Department of Lanzhou University Eluviation Outputs
Download Source Code This example is similar to Windows resource manager. The program running interface is shown in Figure 1: Figure 1 The main class is: ClistCtrl, ctreectrl, cimagelist, cfilefind, and function SHGETFILEINFO () brief description steps are as follows: 1, Add TreeCtrl TVS_HASBUTTONS, TVS_HASLINES, TVS_LINESTROOT STYLE, code as follows:
DWORD DWSTYLE = getWindowlong (m_tree.m_hwnd, gwl_style);
DWStyle | = TVS_HASBUTTONS | TVS_HASLINES | TVS_LINESATROOT
Setwindowlong (m_tree.m_hwnd, gwl_style, dwstyle);
2, add the root item for TreeCtrl:
m_hroot = m_tree.insertitem ("My Computer");
InsertItem () function is original
HtreeItem InsertItem (LPCTSTR LPSZITEM, HTREEITEM HPARENT = TVi_Root,
HtreeItem hinsetAfter = TVi_last);
3. Get local logic drives and add:
Void ctreeViewdlg :: getLogicalDrives (HTREEITEM HPARENT)
{
Size_t szallDrivestrings = getLogicalDriveStrings (0, null);
Char * pdrivestrings = new char [szallDrivestrings sizeof (_t ("))];
GetLogicalDriveStrings (SzallDrivestrings, PDRIVESTRINGS);
SIZE_T SZDRIVESTRING = Strlen (PDRIVESTRINGS);
While (szdriveString> 0)
{
m_tree.insertitem (PDRIVESTRINGS, HPARENT);
PDRIVESTRINGS = SZDRIVESTRING 1;
SZDRIVESTRING = Strlen (PDRIVESTRINGS);
}
}
4, add a TVN_expanded message processing function, add the next level of the directory for its sub-items when an expand:
Void ctreeViewdlg :: OnItemExpandedTree (NmHDR * PNMHDR, LRESULT * PRESULT)
{
NM_TreeView * pnmtreeview = (nm_treeview *) PNMHDR;
// Todo: Add Your Control Notification Handler Code Here
TVitem item = pnmtreeview-> itemnew;
IF (item.hitem == m_hroot)
Return;
HtreeItem hchild = m_tree.getchildItem (item.hitem);
While (hchild)
{
Addsubdir (HCHILD);
Hchild = m_tree.getNextItem (hchild, tvgn_next);
}
* PRESULT = 0;
} AddSubdir function function adds sub-items, and see examples of specific code.
5, add the TVN_Selchanged message processing function, in this function, get the absolute path of the selected item with getFullPath () (GetFullPath () specific code, add a file in ListCtr, not a folder icon: 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.Indirectory () &&! 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.chicon);
M_List.insertItem (i, info.szdisplayname, i);
}
}
* PRESULT = 0;
} This is just a simple example, you can add the mouse to the message to double-click the handler in ListCtrl, open this selected file with Process; this example is compiled under VC6, XP.
VC beginners, if there is any deficiencies, please let mear (Waysen01@st.lzu.edu.cn).