树形控件--浏览文件夹

来源:互联网 发布:男生宿舍捅死同学知乎 编辑:程序博客网 时间:2024/05/19 22:51

主要是在对话框上放了一个树形控件,图标是加载进来的:

注意选中treecontrol的标志haslines,hasbuttons

// FileTreeDlg.h : header file
//

#if !defined(AFX_FILETREEDLG_H__BB3E545E_C6D7_4412_BC60_C99459B61554__INCLUDED_)
#define AFX_FILETREEDLG_H__BB3E545E_C6D7_4412_BC60_C99459B61554__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

/////////////////////////////////////////////////////////////////////////////
// CFileTreeDlg dialog

class CFileTreeDlg : public CDialog
{
// Construction
public:
 CFileTreeDlg(CWnd* pParent = NULL); // standard constructor

// Dialog Data
 //{{AFX_DATA(CFileTreeDlg)
 enum { IDD = IDD_FILETREE_DIALOG };

 //树形控件

 CTreeCtrl m_FileTree;

 //}}AFX_DATA

 // ClassWizard generated virtual function overrides
 //{{AFX_VIRTUAL(CFileTreeDlg)
 protected:
 virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
 //}}AFX_VIRTUAL

// Implementation

protected:

 void BrowseFile ( CString strFile,HTREEITEM ParentNode );

 HICON m_hIcon;

 CImageList m_iImageList;//图标链

 HTREEITEM strHTCur;

 // Generated message map functions
 //{{AFX_MSG(CFileTreeDlg)
 virtual BOOL OnInitDialog();
 afx_msg void OnPaint();
 afx_msg HCURSOR OnQueryDragIcon();
 //}}AFX_MSG
 DECLARE_MESSAGE_MAP()
};

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_FILETREEDLG_H__BB3E545E_C6D7_4412_BC60_C99459B61554__INCLUDED_)

 

// FileTreeDlg.cpp : implementation file
//

#include "stdafx.h"
#include "FileTree.h"
#include "FileTreeDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CFileTreeDlg dialog

CFileTreeDlg::CFileTreeDlg(CWnd* pParent /*=NULL*/)
: CDialog(CFileTreeDlg::IDD, pParent)
{
 //{{AFX_DATA_INIT(CFileTreeDlg)
 // NOTE: the ClassWizard will add member initialization here
 //}}AFX_DATA_INIT
 // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CFileTreeDlg::DoDataExchange(CDataExchange* pDX)
{
 CDialog::DoDataExchange(pDX);
 //{{AFX_DATA_MAP(CFileTreeDlg)
 DDX_Control(pDX, IDC_FILETREE, m_FileTree);
 //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CFileTreeDlg, CDialog)
//{{AFX_MSG_MAP(CFileTreeDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFileTreeDlg message handlers

BOOL CFileTreeDlg::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
 
 //ImageList创建了一个24*24的一幅位图,增长量为0
 
 m_iImageList.Create(24, 24, TRUE,1, 0);
 
 HICON hIcon = NULL;
 
 hIcon = (HICON)::LoadImage(::AfxGetInstanceHandle(),
  MAKEINTRESOURCE(IDI_KEBIAO), IMAGE_ICON, 24, 24, 0);
 
 m_iImageList.Add(hIcon);
 
 //在CTreeCtrl控件中设置ImageList为创建的那个ImageList
 
 m_FileTree.SetImageList ( &m_iImageList,TVSIL_NORMAL );
 
 BrowseFile("成绩表", NULL);//遍历"成绩表"文件夹内的所有目录
 
 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 CFileTreeDlg::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 CFileTreeDlg::OnQueryDragIcon()
{
 return (HCURSOR) m_hIcon;
}

void CFileTreeDlg::BrowseFile( CString strFile,HTREEITEM ParentNode)
{
 CFileFind ff;
 
 CString szDir = strFile;
 
 if ( szDir.Right(1) != "//" )
 {
  szDir += "//";
 }
 
 szDir += "*.*";
 
 BOOL res = ff.FindFile(szDir);
 
 while ( res )
 {
  res = ff.FindNextFile();
  
  /*
  
    if(finder.IsDirectory()   &&   !finder.IsDots())   //如果是目录 
   
  IsDirectory判断是否为目录  
   
     IsDots判断是否为点--这个要说明一下,你用过Dos的话,就应该知道,
    
  每个目录下都有缺省的两个目录,名称分别为'.'和'..',分别代表上一层
    
  目录和本层目录。因此,当我们在遍历目录下文件时,需要过滤掉这两个缺省目录。
     
  */
  
  CString strTitle = ff.GetFileTitle();
  
  //如果是目录而且不是缺省的那两个目录,则递归搜索之
  
  if ( ff.IsDirectory() && !ff.IsDots() )
  {
   //如果是一个子目录,用递归继续往深一层找
   
   CString strPath = ff.GetFilePath();
   
   strHTCur = m_FileTree.InsertItem(strTitle,0,0, ParentNode);
           
   BrowseFile( strPath, strHTCur );
  }
  else if ( !ff.IsDirectory() && !ff.IsDots() )
  {
   //显示当前访问的文件
   
   m_FileTree.InsertItem(strTitle,0,0, ParentNode );
  }
 }
 
 ff.Close();//关闭

}

 

原创粉丝点击