CTreeView 类的使用

首先在源文件中添加下列代码:

void CMytree::OnInitialUpdate()
{
CTreeView::OnInitialUpdate();

// TODO: 在此添加专用代码和/或调用基类
m_ptheTree = &GetTreeCtrl();  
m_ptheTree->ModifyStyle(0, TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS);
m_pImageList = new CImageList();   // 树子节点的图标

CWinApp* pApp=AfxGetApp();
m_pImageList->Create(16,16,ILC_COLOR8|ILC_MASK,9,9);
m_pImageList->Add(pApp->LoadIcon(IDI_MY));
m_ptheTree->SetImageList(m_pImageList, TVSIL_NORMAL);

HTREEITEM hTreeItem;  

hTreeItem = m_ptheTree->InsertItem(L"我的电脑",0,0);
InitTreeView(hTreeItem);
}

再在类中添加 InitTreeView 函数,完成子项的添加:

void CMytree::InitTreeView(HTREEITEM hParent)
{
int nPos = 0;
UINT nCount = 0;
CString strDrive = L"?:\\";
DWORD dwDriveList = ::GetLogicalDrives();
CString cTmp;
while (dwDriveList)
{
   if (dwDriveList & 1)
   {
    cTmp = strDrive;
    strDrive.SetAt (0, 0x41 + nPos);
    if (AddDrives(strDrive , hParent))
     nCount++;
   }
   dwDriveList >>= 2;
   nPos++;
}
return;

}

BOOL CMytree::AddDrives(CString strDrive, HTREEITEM hParent)
{
HTREEITEM hItem;
UINT nType = ::GetDriveType ((LPCTSTR) strDrive);
UINT nDrive = (UINT) strDrive[0] - 0x41;
switch (nType)
{
case DRIVE_REMOVABLE:
   hItem = GetTreeCtrl().InsertItem(strDrive, 0, 0, hParent);
   AddDummyNode(hItem);
   break;
case DRIVE_FIXED:
   hItem = GetTreeCtrl().InsertItem(strDrive, 0, 0, hParent);
   AddDummyNode(hItem);
   break;
case DRIVE_REMOTE:
   hItem = GetTreeCtrl().InsertItem(strDrive, 0, 0, hParent);
   AddDummyNode(hItem);
   break;
case DRIVE_CDROM:
   hItem = GetTreeCtrl().InsertItem(strDrive, 0, 0, hParent);
   AddDummyNode(hItem);
   break;
case DRIVE_RAMDISK:
   hItem = GetTreeCtrl().InsertItem(strDrive, 0, 0, hParent);
   AddDummyNode(hItem);
   break;
default:
   return FALSE;
}
return true;
}

void CMytree::AddDummyNode(HTREEITEM hItem)
{
GetTreeCtrl().InsertItem (L"", 0, 0, hItem);
}


你可能感兴趣的:(CTreeView 类的使用)