ChooseDirDlg.cpp
上传用户:netltd
上传日期:2013-02-12
资源大小:7234k
文件大小:7k
- // ChooseDirDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "resource.h"
- #include "ChooseDirDlg.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CChooseDirDlg dialog
- CChooseDirDlg::CChooseDirDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CChooseDirDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CChooseDirDlg)
- m_Path = _T("");
- //}}AFX_DATA_INIT
- }
- void CChooseDirDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CChooseDirDlg)
- DDX_Control(pDX, IDC_DIRECTORY, m_Directory);
- DDX_Text(pDX, IDC_PATH, m_Path);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CChooseDirDlg, CDialog)
- //{{AFX_MSG_MAP(CChooseDirDlg)
- ON_NOTIFY(TVN_ITEMEXPANDING, IDC_DIRECTORY, OnItemexpandingDirectory)
- ON_NOTIFY(TVN_SELCHANGED, IDC_DIRECTORY, OnSelchangedDirectory)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CChooseDirDlg message handlers
- //获得选定的路径
- CString CChooseDirDlg::GetDirectory(CString path)
- {
- m_Path = path;
- DoModal();
- return m_Path;
- }
- int CChooseDirDlg::DoModal()
- {
- // TODO: Add your specialized code here and/or call the base class
- //加入图标
- m_ImageList.Create(16, 16, ILC_MASK, 2, 2);
- m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_MYCOMPUTER));
- m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_FLOPPY));
- m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_HARDDISK));
- m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_CDROM));
- m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_CLSFOLD));
- m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_OPENFOLD));
- return CDialog::DoModal();
- }
- BOOL CChooseDirDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
-
- // TODO: Add extra initialization here
-
- HTREEITEM hLevel;
- //初始化目录列表框
- m_Directory.SetImageList(&m_ImageList, TVSIL_NORMAL);
- hLevelRoot = m_Directory.InsertItem("我的电脑",
- 0, 0, TVI_ROOT, TVI_SORT);
- char strDriver[_MAX_PATH];
- char temp[_MAX_PATH];
- int index =0 ;
- int IconIndex;
- CString DriverName;
- if(GetLogicalDriveStrings(MAX_PATH, strDriver)==0)
- return TRUE;
- for(;;)
- {
- strncpy(temp, strDriver+index, 2);
- //加入字符串结束标志
- temp[2] = NULL;
- index = index + 4;
-
- switch( GetDriveType(temp) )
- {
- case DRIVE_REMOVABLE:
- IconIndex = 1;
- break;
- case DRIVE_CDROM:
- IconIndex = 3;
- break;
- default:
- IconIndex = 2;
- }
- DriverName = temp;
- DriverName.MakeUpper();
- //不显示 A 驱
- if(DriverName != "A:")
- {
- hLevel= m_Directory.InsertItem((LPCTSTR)DriverName,
- IconIndex, IconIndex, hLevelRoot);
- FindDir(hLevel, DriverName);
- }
- if(strDriver[index] == NULL)
- break;
- }
- m_Directory.Expand(hLevelRoot, TVE_EXPAND);
- ExpandPath(m_Path);
- SetDlgItemText(IDC_PATH, m_Path);
- return TRUE; // return TRUE unless you set the focus to a control
- // EXCEPTION: OCX Property Pages should return FALSE
- }
- void CChooseDirDlg::FindDir(HTREEITEM level, CString strDirect)
- {
- HANDLE hFind;
- WIN32_FIND_DATA fd;
- HTREEITEM Childlevel;
- CString direct;
- CString filename;
- strDirect += "\*.*";
- //查找指定目录下的的所有文件,包括子目录
- if( ( hFind = FindFirstFile(strDirect , &fd) ) == INVALID_HANDLE_VALUE )
- return;
- while(TRUE)
- {
- if( (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0)
- {
- //不显示".",".."这两个目录
- if(fd.cFileName[0] == '.')
- ;
- else
- {
- filename = fd.cFileName;
- Childlevel = m_Directory.InsertItem((LPCSTR)filename,
- 4, 5, level);
- }
- }
- //查找下一个文件
- if(FindNextFile(hFind, &fd) == 0)
- {
- FindClose(hFind);
- //对level的子项排序
- m_Directory.SortChildren(level);
- break;
- }
- }
- }
- void CChooseDirDlg::OnItemexpandingDirectory(NMHDR* pNMHDR, LRESULT* pResult)
- {
- NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
- // TODO: Add your control notification handler code here
- HTREEITEM hLevel;
- hLevel = pNMTreeView->itemNew.hItem;
- if( pNMTreeView->action == TVE_EXPAND)
- {
- if((hLevel = m_Directory.GetChildItem(hLevel)) == NULL)
- return;
- else
- if(!m_Directory.ItemHasChildren(hLevel))
- FindDir(hLevel, GetstrDirect(hLevel));
- while((hLevel = m_Directory.GetNextSiblingItem(hLevel))
- !=NULL)
- {
- if(!m_Directory.ItemHasChildren(hLevel))
- FindDir(hLevel, GetstrDirect(hLevel));
- }
- }
- *pResult = 0;
- }
- /*保留
- void CChooseDirDlg::FindAllDir(HTREEITEM level, CString strDirect)
- {
- HTREEITEM parentlevel;
- HTREEITEM friendlevel;
- CString str;
- CString filename;
- parentlevel = level;
- str = strDirect;
- while(TRUE)
- {
- FindDir(parentlevel, str);
- if(!m_Directory.ItemHasChildren(parentlevel))
- {
- if(parentlevel==level)
- return;
- filename = m_Directory.GetItemText(parentlevel);
- //have problem,use "Delete" function, such as
- //CString s1 = "abcaabd", s2 = "abc";
- //s1.TrimLeft((LPCTSTR)s2);
- //s1={"bd"};
- str.TrimRight((LPCTSTR)filename);
- str.TrimRight("\");
- while((friendlevel = m_Directory.GetNextSiblingItem(parentlevel))
- ==NULL)
- {
- parentlevel = m_Directory.GetParentItem(parentlevel);
- filename = m_Directory.GetItemText(parentlevel);
- str.TrimRight((LPCTSTR)filename);
- str.TrimRight("\");
- if(parentlevel==level)
- return;
- }
- parentlevel = friendlevel;
- filename = m_Directory.GetItemText(parentlevel);
- str = str + "\" + filename;
- continue;
- }
- parentlevel = m_Directory.GetChildItem(parentlevel);
- str += "\" + m_Directory.GetItemText(parentlevel);
- }
- }
- */
- CString CChooseDirDlg::GetstrDirect(HTREEITEM level)
- {
- CString str;
- str = m_Directory.GetItemText(level);
- while((level = m_Directory.GetParentItem(level)) != hLevelRoot)
- {
- str = m_Directory.GetItemText(level) + "\" + str;
- }
- return str;
- }
- void CChooseDirDlg::ExpandPath(CString path)
- {
- CString str;
- CString s;
- int index;
- HTREEITEM level;
- level = m_Directory.GetChildItem(hLevelRoot);
- while((index = path.Find("\")) != -1)
- {
- str = path.Left(index);
- path.Delete(0, index+1);
- if(str == m_Directory.GetItemText(level))
- m_Directory.Expand(level, TVE_EXPAND);
- else
- while(TRUE)
- {
- if((level = m_Directory.GetNextSiblingItem(level))
- ==NULL)
- return;
- s = m_Directory.GetItemText(level);
- if(str.CompareNoCase((LPCTSTR)m_Directory.GetItemText(level))
- == 0)
- {
-
- m_Directory.Expand(level, TVE_EXPAND);
- level = m_Directory.GetChildItem(level);
- break;
- }
- }
- }
- }
- void CChooseDirDlg::OnSelchangedDirectory(NMHDR* pNMHDR, LRESULT* pResult)
- {
- NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
- // TODO: Add your control notification handler code here
-
- //当被选择的目录改变后,改变EDIT 控件的内容
- HTREEITEM hLevel;
- CString str;
- hLevel = pNMTreeView->itemNew.hItem;
- if(hLevel == hLevelRoot)
- return;
- str = GetstrDirect(hLevel);
- if(str.Find("\") == -1)
- str += "\";
- m_Path = str;
- UpdateData(FALSE);
- *pResult = 0;
- }