FileTreeDlg.cpp
Upload User: lgllqt
Upload Date: 2021-08-03
Package Size: 2880k
Code Size: 4k
Category:

TreeView

Development Platform:

Visual C++

  1. // FileTreeDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "FileTree.h"
  5. #include "FileTreeDlg.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CFileTreeDlg dialog
  13. CFileTreeDlg::CFileTreeDlg(CWnd* pParent /*=NULL*/)
  14. : CDialog(CFileTreeDlg::IDD, pParent)
  15. {
  16. //{{AFX_DATA_INIT(CFileTreeDlg)
  17. // NOTE: the ClassWizard will add member initialization here
  18. //}}AFX_DATA_INIT
  19. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  20. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  21. }
  22. void CFileTreeDlg::DoDataExchange(CDataExchange* pDX)
  23. {
  24. CDialog::DoDataExchange(pDX);
  25. //{{AFX_DATA_MAP(CFileTreeDlg)
  26. DDX_Control(pDX, IDC_FILETREE, m_FileTree);
  27. //}}AFX_DATA_MAP
  28. }
  29. BEGIN_MESSAGE_MAP(CFileTreeDlg, CDialog)
  30. //{{AFX_MSG_MAP(CFileTreeDlg)
  31. ON_WM_PAINT()
  32. ON_WM_QUERYDRAGICON()
  33. //}}AFX_MSG_MAP
  34. END_MESSAGE_MAP()
  35. /////////////////////////////////////////////////////////////////////////////
  36. // CFileTreeDlg message handlers
  37. BOOL CFileTreeDlg::OnInitDialog()
  38. {
  39. CDialog::OnInitDialog();
  40. // Set the icon for this dialog.  The framework does this automatically
  41. //  when the application's main window is not a dialog
  42. SetIcon(m_hIcon, TRUE); // Set big icon
  43. SetIcon(m_hIcon, FALSE); // Set small icon
  44. m_iImageList.Create(24, 24, TRUE,1, 0);
  45. HICON hIcon = NULL;
  46. hIcon = (HICON)::LoadImage(::AfxGetInstanceHandle(), 
  47. MAKEINTRESOURCE(IDI_KEBIAO), IMAGE_ICON, 24, 24, 0);
  48. m_iImageList.Add(hIcon);
  49. m_FileTree.SetImageList ( &m_iImageList,TVSIL_NORMAL );
  50. BrowseFile(0,"成绩表");//遍历"成绩表"文件夹内的所有目录
  51. return TRUE;  // return TRUE  unless you set the focus to a control
  52. }
  53. // If you add a minimize button to your dialog, you will need the code below
  54. //  to draw the icon.  For MFC applications using the document/view model,
  55. //  this is automatically done for you by the framework.
  56. void CFileTreeDlg::OnPaint() 
  57. {
  58. if (IsIconic())
  59. {
  60. CPaintDC dc(this); // device context for painting
  61. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  62. // Center icon in client rectangle
  63. int cxIcon = GetSystemMetrics(SM_CXICON);
  64. int cyIcon = GetSystemMetrics(SM_CYICON);
  65. CRect rect;
  66. GetClientRect(&rect);
  67. int x = (rect.Width() - cxIcon + 1) / 2;
  68. int y = (rect.Height() - cyIcon + 1) / 2;
  69. // Draw the icon
  70. dc.DrawIcon(x, y, m_hIcon);
  71. }
  72. else
  73. {
  74. CDialog::OnPaint();
  75. }
  76. }
  77. // The system calls this to obtain the cursor to display while the user drags
  78. //  the minimized window.
  79. HCURSOR CFileTreeDlg::OnQueryDragIcon()
  80. {
  81. return (HCURSOR) m_hIcon;
  82. }
  83. void CFileTreeDlg::BrowseFile(int CallNum, CString strFile)
  84. {
  85. CallNum++;
  86. CFileFind ff;
  87. CString szDir = strFile;
  88. if(szDir.Right(1) != "\")
  89. szDir += "\";
  90. szDir += "*.*";
  91. BOOL res = ff.FindFile(szDir);
  92. while(res)
  93. {
  94. res = ff.FindNextFile();
  95. if(ff.IsDirectory() && !ff.IsDots())
  96. {
  97. //如果是一个子目录,用递归继续往深一层找
  98. CString strPath = ff.GetFilePath();
  99. CString strTitle = ff.GetFileTitle();
  100. int i =0;
  101. switch(CallNum)
  102. {
  103. case 1:
  104. strHTFir = m_FileTree.InsertItem(strTitle,0,0,NULL);
  105. break;
  106. case 2:
  107. strHTSec = m_FileTree.InsertItem(strTitle,0,0,strHTFir);
  108. break;
  109. case 3:
  110. strHtThi = m_FileTree.InsertItem(strTitle,0,0,strHTSec);
  111. break;
  112. case 4:
  113. strHtFor = m_FileTree.InsertItem(strTitle,0,0,strHtThi);
  114. break;
  115. default:
  116. strHtFif = m_FileTree.InsertItem(strTitle,0,0,strHtFor);
  117. break;
  118. }
  119. BrowseFile(CallNum,strPath);
  120. }
  121. else if(!ff.IsDirectory() && !ff.IsDots())
  122. {
  123. //显示当前访问的文件
  124. CString strPath;
  125. CString strTitle;
  126. strPath = ff.GetFilePath();
  127. strTitle = ff.GetFileTitle();
  128. switch(CallNum)
  129. {
  130. case 1:
  131. strRoot = m_FileTree.InsertItem(strTitle,0,0,NULL);
  132. break;
  133. case 2:
  134. strHtEnd = m_FileTree.InsertItem(strTitle,0,0,strHTFir);
  135. break;
  136. case 3:
  137. strHtEnd = m_FileTree.InsertItem(strTitle,0,0,strHTSec);
  138. break;
  139. case 4:
  140. strHtEnd = m_FileTree.InsertItem(strTitle,0,0,strHtThi);
  141. break;
  142. case 5:
  143. strHtEnd = m_FileTree.InsertItem(strTitle,0,0,strHtFor);
  144. break;
  145. default:
  146. strHtEnd = m_FileTree.InsertItem(strTitle,0,0,strHtFif);
  147. break;
  148. }
  149. }
  150. }
  151. ff.Close();//关闭
  152. }