转自:http://www.sicaril.com/thread-2104-1-1.html
- //key code
- HANDLE g_Event;
- VOID GetFile(CTreeCtrl& TreeCtrl, LPSTR lpPath, HTREEITEM hParentsItem)
- {
- WIN32_FIND_DATA FindFileData = {0};
- char szFileName[MAX_PATH] = {0};
- ASSERT(lpPath);
- ASSERT(hParentsItem);
- sprintf(szFileName, "%s%s", lpPath, "*.*");
- HANDLE hFile = FindFirstFile(szFileName, &FindFileData);
- if (hFile == INVALID_HANDLE_VALUE)
- {
- return;
- }
- do
- {
- WIN32_FILE_ATTRIBUTE_DATA a;
- FILETIME FileTime;
- SYSTEMTIME sysTime;
- char ch = '.';
- char* lpch = "..";
- if (
- (FindFileData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY)&&
- (strstr(FindFileData.cFileName, &ch) == NULL)&&
- (strstr(FindFileData.cFileName, lpch) == NULL)
- )
- {
- //printf("%s\r\n", FindFileData.cFileName);
- FileTimeToLocalFileTime(&a.ftCreationTime, &FileTime);
- FileTimeToSystemTime(&FileTime, &sysTime);
- //printf("\t\t");
- //printf("%d-%d-%d\t", sysTime.wYear, sysTime.wMonth, sysTime.wDay);
- TreeCtrl.InsertItem(FindFileData.cFileName, hParentsItem);
- }
- else
- {
- //printf("------%s-----", FindFileData.cFileName);
- //printf("\r\n");
- char szPath[MAX_PATH] = {0};
- memset(szPath, 0, MAX_PATH);
- sprintf(szPath, "%s\\%s\\%s", lpPath,FindFileData.cFileName, "*.*");
- GetFile(TreeCtrl, szPath, TreeCtrl.InsertItem(FindFileData.cFileName, hParentsItem));
- }
- } while (FindNextFile(hFile, &FindFileData));
-
- if (hFile != INVALID_HANDLE_VALUE)
- {
- FindClose(hFile);
- hFile = INVALID_HANDLE_VALUE;
- }
-
- }
-
- VOID Traveser(CTreeCtrl& TreeCtrl)
- {
- DWORD dwDrives = GetLogicalDrives();
- if (dwDrives <= 0)
- {
- return;
- }
- char szbuf[MAXBYTE] = {0};
- if (GetLogicalDriveStrings(MAXBYTE, szbuf) != 0)
- {
- char* lpRootPathName = szbuf;
-
- for (int i = 0; i < MAXBYTE; i++)
- {
- if ((lpRootPathName[i] >= 'C' && lpRootPathName[i] <= 'Z')
- || (lpRootPathName[i] >= 'c' && lpRootPathName[i] <= 'z'))
- {
- char szFormat[MAX_PATH] = {0};
- sprintf(szbuf, "%c:\\", lpRootPathName[i]);
- GetFile(TreeCtrl, szbuf, TreeCtrl.InsertItem(szbuf));
- }
- }
- }
- SetEvent(g_Event);
- }
-
- DWORD __stdcall ThreadProc(LPARAM lpParam)
- {
- CTreeCtrl* lpTree = (CTreeCtrl*)lpParam;
- ASSERT(lpTree);
- lpTree->DeleteAllItems();
- lpTree->EnableAutomation();
- lpTree->EnableWindow(FALSE);
- lpTree->SetBkColor(RGB(78,155,177));
- lpTree->SetTextColor(RGB(155, 0,0));
- lpTree->SetRedraw(FALSE);
- g_Event = CreateEvent(NULL, FALSE, FALSE, NULL);
- Traveser(*lpTree);
- WaitForSingleObject(g_Event, -1);
- lpTree->EnableWindow();
- lpTree->SetRedraw(TRUE);
- return 0;
- }
- BOOL CFileDirctoryDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
- ASSERT(IDM_ABOUTBOX < 0xF000);
- CMenu* pSysMenu = GetSystemMenu(FALSE);
- if (pSysMenu != NULL)
- {
- CString strAboutMenu;
- strAboutMenu.LoadString(IDS_ABOUTBOX);
- if (!strAboutMenu.IsEmpty())
- {
- pSysMenu->AppendMenu(MF_SEPARATOR);
- pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
- }
- }
- SetIcon(m_hIcon, TRUE);
- SetIcon(m_hIcon, FALSE);
- m_hThread = CreateThread(NULL,
- NULL,
- (DWORD (__stdcall*)(void *))ThreadProc,
- &m_FileTree,
- CREATE_SUSPENDED,
- &m_dwPid);
-
- if (m_hThread != NULL)
- {
- ResumeThread(m_hThread);
- CloseHandle(m_hThread);
- }
-
-
- return TRUE; // return TRUE unless you set the focus to a control
- }
还有很多功能没做完,待续