vc中treeview显示磁盘目录和文件代码

/ CTreeViewDlg message handlers
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//º¯Êý¹¦ÄÜ:³õʼ»¯
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
BOOL CTreeViewDlg::OnInitDialog()
{
 CDialog::OnInitDialog();

 // Set the icon for this dialog.  The framework does this automatically
 //  when the application's main window is not a dialog
 SetIcon(m_hIcon, TRUE);   // Set big icon
 SetIcon(m_hIcon, FALSE);  // Set small icon
 
 // TODO: Add extra initialization here
  m_ImageList.Create(32,32,ILC_COLOR32,10,30);
 m_list.SetImageList(&m_ImageList,LVSIL_NORMAL);
 DWORD dwStyle = GetWindowLong(m_tree.m_hWnd,GWL_STYLE);
 dwStyle |= TVS_HASBUTTONS | TVS_HASLINES | TVS_LINESATROOT;
 SetWindowLong(m_tree.m_hWnd,GWL_STYLE,dwStyle);
    m_hRoot = m_tree.InsertItem("ÎҵĵçÄÔ");
 GetLogicalDrives(m_hRoot);
 GetDriveDir(m_hRoot);
 m_tree.Expand(m_hRoot,TVE_EXPAND);
 return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CTreeViewDlg::OnPaint()
{
 if (IsIconic())
 {
  CPaintDC dc(this); // device context for painting

  SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

  // Center icon in client rectangle
  int cxIcon = GetSystemMetrics(SM_CXICON);
  int cyIcon = GetSystemMetrics(SM_CYICON);
  CRect rect;
  GetClientRect(&rect);
  int x = (rect.Width() - cxIcon + 1) / 2;
  int y = (rect.Height() - cyIcon + 1) / 2;

  // Draw the icon
  dc.DrawIcon(x, y, m_hIcon);
 }
 else
 {
  CDialog::OnPaint();
 }
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CTreeViewDlg::OnQueryDragIcon()
{
 return (HCURSOR) m_hIcon;
}
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//º¯Êý¹¦ÄÜ:»ñÈ¡Çý¶¯Æ÷
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
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);
 }
}
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//º¯Êý¹¦ÄÜ:Ìí¼Ó×ÓÏî
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

void CTreeViewDlg::GetDriveDir(HTREEITEM hParent)
{
    HTREEITEM hChild = m_tree.GetChildItem(hParent);
 while(hChild)
 {
        CString strText = m_tree.GetItemText(hChild);
  if(strText.Right(1) != "//")
   strText += _T("//");
  strText += "*.*";
  CFileFind file;
     BOOL bContinue = file.FindFile(strText);
  while(bContinue)
  {
            bContinue = file.FindNextFile();
   if(file.IsDirectory() && !file.IsDots())
          m_tree.InsertItem(file.GetFileName(),hChild);
  }
  GetDriveDir(hChild);
  hChild = m_tree.GetNextItem(hChild,TVGN_NEXT);
 } 
}
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//º¯Êý¹¦ÄÜ:Õ¹¿ªÊ¼þº¯Êý
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
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;
}
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//º¯Êý¹¦ÄÜ:»ñÈ¡Ê÷ÏîĿȫ¸ú¾¶
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
CString CTreeViewDlg::GetFullPath(HTREEITEM hCurrent)
{
    CString strTemp;
 CString strReturn = "";
 while(hCurrent != m_hRoot)
 {
  strTemp = m_tree.GetItemText(hCurrent);
  if(strTemp.Right(1) != "//")
   strTemp += "//";
  strReturn = strTemp  + strReturn;
  hCurrent = m_tree.GetParentItem(hCurrent);
 }
 return strReturn;
}
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//Ìí¼Ó×ÓĿ¼
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void CTreeViewDlg::AddSubDir(HTREEITEM hParent)
{
    CString strPath = GetFullPath(hParent);
 if(strPath.Right(1) != "//")
  strPath += "//";
 strPath += "*.*";
 CFileFind file;
 BOOL bContinue = file.FindFile(strPath);
 while(bContinue)
 {
  bContinue = file.FindNextFile();
  if(file.IsDirectory() && !file.IsDots())
      m_tree.InsertItem(file.GetFileName(),hParent);
 }
}
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//º¯Êý¹¦ÄÜ:Ñ¡ÖÐʼþ
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
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;
}

void CTreeViewDlg::OnSelchangingTree(NMHDR* pNMHDR, LRESULT* pResult)
{

 *pResult = 0;
}

void CTreeViewDlg::OnClickTree(NMHDR* pNMHDR, LRESULT* pResult)
{
 // TODO: Add your control notification handler code here

 *pResult = 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值