用什么函数得到所选择文件夹的路径啊?比如说C:/,C:/WINDOWS
用一个编辑框,一个按纽,个一个LIST控件 点按纽出现一个文件选择框,相当于一个CFILE,选择后在EDIT里显示其路径(不是文件的路径,而是文件夹的路径)
怎么实现把该文件夹下的所有文件名(包括子文件夹,比如说选择了C:/那么C:/AA里的文件也要显示出来)在一个LIST控件里显示啊?
小弟初学,请各位前辈指教
用一个编辑框,一个按纽,个一个LIST控件 点按纽出现一个文件选择框,相当于一个CFILE,选择后在EDIT里显示其路径(不是文件的路径,而是文件夹的路径)
怎么实现把该文件夹下的所有文件名(包括子文件夹,比如说选择了C:/那么C:/AA里的文件也要显示出来)在一个LIST控件里显示啊?
小弟初学,请各位前辈指教
解决方案 »
- 求助!QQ的网络设置文件放哪里?
- Windows控制台编程
- 《COM本质论》的问题
- ODBC API错误
- 求:vc的类似于qq的音频、视频聊天程序
- Create了一个Button之后如何让他在窗口里显示出来???
- DEBUG时没问题,RELEASE时出现afxver_.h(130) : fatal error C1189: #error : Please use the /MD switch for _AFXDLL builds
- 在模板中定义static类型数据时出错?
- beijingshizi(菜青虫)、masterdog(掌门狗)、white_whan(C++ Fans) 、fz_zhou(vcfan) 请取分
- 实时显示波形不流畅的问题?
- 怎么判断一个线程是否已经死了?
- 如何在Visual C++ 6下实现自动隐藏的窗体(Auto Hide Window or Docking Pane)?
#include <iostream>using namespace std;void Recurse(LPCTSTR pstr)
{
CFileFind finder;// build a string with wildcards
CString strWildcard(pstr);
strWildcard += _T("\\*.*");// start working for files
BOOL bWorking = finder.FindFile(strWildcard);while (bWorking)
{
bWorking = finder.FindNextFile();// skip . and .. files; otherwise, we'd
// recur infinitely!if (finder.IsDots())
continue;// if it's a directory, recursively search itif (finder.IsDirectory())
{
CString str = finder.GetFilePath();
cout << (LPCTSTR) str << endl;
Recurse(str);
}
}finder.Close();
}void main()
{
if (!AfxWinInit(GetModuleHandle(NULL), NULL, GetCommandLine(), 0))
cout << "panic!" << endl;
else
Recurse(_T("C:"));
}
m_list.InsertColumn( 0, "ID", LVCFMT_LEFT, 40 );//插入列
int nRow = m_list.InsertItem(0, path);//插入行
m_list.SetItemText(nRow, 1, strtemp);//设置每项数据具体的函数参数请参考msdn
// CFileTreeView viewclass CFileTreeView : public CView
{
protected:
CFileTreeView(); // protected constructor used by dynamic creation
DECLARE_DYNCREATE(CFileTreeView)// Attributes
public:// Operations
public:
void OnFiletreeRefresh();
void HideButton(HTREEITEM hti);
BOOL HaveSubDirOrFile(char *szCurDir);
BOOL GetSubSiblingItem(HTREEITEM hItem, char *szCurDir);
CString GetFileExName(TCHAR cFileName[MAX_PATH]);
void DisplayButton(HTREEITEM hti);
void InitRootDirectory();
CTreeCtrl m_tree;
CImageList m_ImageList;
CString m_strOpenFile;// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CFileTreeView)
public:
protected:
virtual void OnDraw(CDC* pDC); // overridden to draw this view
virtual void OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView);
//}}AFX_VIRTUAL// Implementation
protected:
virtual ~CFileTreeView();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif // Generated message map functions
protected:
//{{AFX_MSG(CFileTreeView)
afx_msg void OnItemexpanding(NMHDR* pNMHDR, LRESULT* pResult);
afx_msg void OnDblclk(NMHDR* pNMHDR, LRESULT* pResult);
afx_msg void OnSize(UINT nType, int cx, int cy);
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
void CFileTreeView::InitRootDirectory()
{
HTREEITEM hItem,hti;
CStringArray arDriverName;
CString strDriverName;
CString str;
UINT nDriverType; if (m_tree.GetImageList(TVSIL_NORMAL) == NULL)
m_tree.SetImageList(&m_ImageList,TVSIL_NORMAL); hItem = m_tree.InsertItem(_T("ÎҵĵçÄÔ"),0,0,TVI_ROOT,TVI_LAST); char szDriverName[500];
char szDriverLabel[500];
memset(szDriverName,0,sizeof(szDriverName));
memset(szDriverLabel,0,sizeof(szDriverLabel));
DWORD nLength = GetLogicalDriveStrings(sizeof(szDriverName),szDriverName);
for (int i=0; i<(int)nLength; i++)
{
if (szDriverName[i] != '\0')
strDriverName += szDriverName[i];
else
{
strDriverName = strDriverName.Left(strDriverName.GetLength() - 1);
arDriverName.Add(strDriverName);
strDriverName = "";
}
} char *szCurDir = NULL;
for (i=0; i<arDriverName.GetSize(); i++)
{
nDriverType = GetDriveType((LPCTSTR)arDriverName.GetAt(i));
GetVolumeInformation((LPCTSTR)(arDriverName.GetAt(i) + "\\"),szDriverLabel,\
sizeof(szDriverLabel),NULL,NULL,0,NULL,0);
hti = m_tree.InsertItem((CString)szDriverLabel + "(" + arDriverName.GetAt(i) + ")",\
nDriverType,nDriverType,hItem,TVI_LAST); DisplayButton(hti);
memset(szDriverLabel,0,sizeof(szDriverLabel));
} hti = m_tree.GetRootItem();
m_tree.SelectItem(hti);
m_tree.Expand(hti,TVE_EXPAND);
}void CFileTreeView::DisplayButton(HTREEITEM hti)
{
TVITEM tvi;
tvi.mask = TVIF_CHILDREN;
tvi.hItem = hti;
tvi.cChildren = 1; m_tree.SetItem(&tvi);
}CString CFileTreeView::GetFileExName(TCHAR cFileName[MAX_PATH])
{
CString strFileName,strFileExName; strFileName = (CString)cFileName;
strFileExName.Empty();
for (int i=strFileName.GetLength()-1; i>=0; i--)
{
if (strFileName.GetAt(i) != '.')
strFileExName = strFileName.GetAt(i) + strFileExName;
else
return strFileExName;
}
return strFileExName;
}BOOL CFileTreeView::GetSubSiblingItem(HTREEITEM hItem, char *szCurDir)
{
BOOL bHaveChild = FALSE;
CString strEx;
HANDLE hFind;
HTREEITEM hti;
WIN32_FIND_DATA wfd; if (_chdir(szCurDir) != 0) return bHaveChild;
hFind = FindFirstFile(_T("*.*"),&wfd);
if(hFind == INVALID_HANDLE_VALUE) return bHaveChild; if ((wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
!(wfd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN))
{
if( wfd.cFileName[0] != '.' )
{
bHaveChild = TRUE;
hti = m_tree.InsertItem(wfd.cFileName,6,6,hItem,TVI_FIRST);
m_tree.SetItemData(hti,DIR);
DisplayButton(hti);
_chdir(".."); // ²éÕÒÍê±ÏÖ®ºó, ·µ»ØÉÏÒ»¼¶Ä¿Â¼
}
}
else
{
strEx = GetFileExName(wfd.cFileName);
strEx.MakeUpper();
if (!(wfd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN))
{
if (strEx == "RTF")
{
hti = m_tree.InsertItem(wfd.cFileName,8,8,hItem,TVI_LAST);
m_tree.SetItemData(hti,FILE);
bHaveChild = TRUE;
}
else if (strEx == "TXT" || strEx == "LOG" || strEx == "INI" || strEx == "HTM" ||
strEx == "HTML"|| strEx == "CPP" || strEx == "H" || strEx == "JAVA"||
strEx == "C" || strEx == "CSS" || strEx == "DIC" || strEx == "INF")
{
hti = m_tree.InsertItem(wfd.cFileName,7,7,hItem,TVI_LAST);
m_tree.SetItemData(hti,FILE);
bHaveChild = TRUE;
}
}
} while(FindNextFile(hFind, &wfd))
{
if ((wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
!(wfd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN))
{
if(wfd.cFileName[0] != '.')
{
bHaveChild = TRUE;
hti = m_tree.InsertItem(wfd.cFileName,6,6,hItem,TVI_FIRST);
m_tree.SetItemData(hti,DIR);
DisplayButton(hti);
_chdir(".."); // ²éÕÒÍê±ÏÖ®ºó, ·µ»ØÉÏÒ»¼¶Ä¿Â¼
}
}
else
{
strEx = GetFileExName(wfd.cFileName);
strEx.MakeUpper();
if (!(wfd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN))
{
if (strEx == "RTF")
{
hti = m_tree.InsertItem(wfd.cFileName,8,8,hItem,TVI_LAST);
m_tree.SetItemData(hti,FILE);
bHaveChild = TRUE;
}
else if (strEx == "TXT" || strEx == "LOG" || strEx == "INI" || strEx == "HTM" ||
strEx == "HTML"|| strEx == "CPP" || strEx == "H" || strEx == "JAVA"||
strEx == "C" || strEx == "CSS" || strEx == "DIC" || strEx == "INF")
{
hti = m_tree.InsertItem(wfd.cFileName,7,7,hItem,TVI_LAST);
m_tree.SetItemData(hti,FILE);
bHaveChild = TRUE;
}
}
}
}
FindClose(hFind);
return bHaveChild;}BOOL CFileTreeView::HaveSubDirOrFile(char *szCurDir)
{
BOOL bHaveChild = FALSE;
HANDLE hFind;
WIN32_FIND_DATA wfd;
CString strEx; if (_chdir(szCurDir) != 0) return bHaveChild;
hFind = FindFirstFile(_T("*.*"),&wfd);
if(hFind == INVALID_HANDLE_VALUE) return bHaveChild; if(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if( wfd.cFileName[0] != '.' )
{
bHaveChild = TRUE;
_chdir(".."); // ²éÕÒÍê±ÏÖ®ºó, ·µ»ØÉÏÒ»¼¶Ä¿Â¼
FindClose(hFind);
return bHaveChild;
}
}
else
{
strEx = GetFileExName(wfd.cFileName);
strEx.MakeUpper();
if (strEx == "TXT" || strEx == "LOG" || strEx == "INI" || strEx == "HTM" ||
strEx == "HTML"|| strEx == "CPP" || strEx == "H" || strEx == "JAVA"||
strEx == "C" || strEx == "CSS" || strEx == "DIC" || strEx == "INF" ||
strEx == "RTF")
{
bHaveChild = TRUE;
FindClose(hFind);
return bHaveChild;
}
} while(FindNextFile(hFind, &wfd))
{
if(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if(wfd.cFileName[0] != '.')
{
bHaveChild = TRUE;
_chdir("..");
FindClose(hFind);
return bHaveChild;
}
}
else
{
strEx = GetFileExName(wfd.cFileName);
strEx.MakeUpper();
if (strEx == "TXT" || strEx == "LOG" || strEx == "INI" || strEx == "HTM" ||
strEx == "HTML"|| strEx == "CPP" || strEx == "H" || strEx == "JAVA"||
strEx == "C" || strEx == "CSS" || strEx == "DIC" || strEx == "INF" ||
strEx == "RTF")
{
bHaveChild = TRUE;
FindClose(hFind);
return bHaveChild;
}
}
}
FindClose(hFind);
return bHaveChild;
}
{
TVITEM tvi;
tvi.mask = TVIF_CHILDREN;
tvi.hItem = hti;
tvi.cChildren = 0; m_tree.SetItem(&tvi);
}void CFileTreeView::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView)
{ CView::OnActivateView(bActivate, pActivateView, pDeactiveView);
}void CFileTreeView::OnDblclk(NMHDR* pNMHDR, LRESULT* pResult)
{
HTREEITEM hti,hParent;
CTreeCtrl &tree = m_tree;
CString strPath,str1,str2; hti = tree.GetSelectedItem();
if (tree.GetItemData(hti) == FILE)
{
strPath = tree.GetItemText(hti);
hParent = tree.GetParentItem(hti);
while (hParent != tree.GetRootItem())
{
strPath = tree.GetItemText(hParent) + "\\" + strPath;
hParent = tree.GetParentItem(hParent);
}
strPath += "\\";
str1 = strPath.Mid(strPath.Find(":") - 2 + 1,2);
str2 = strPath.Right(strPath.GetLength() - strPath.Find(":") - 2);
strPath = str1 + str2;
strPath = strPath.Left(strPath.GetLength() - 1);
m_strOpenFile = strPath; /////////////////////////////////////////////////////////////////////
//ÁÙʱ´ò¿ªÎļþ
// CMDIChildWnd *pMainFrame = ((CMainFrame*)GetParentFrame())->MDIGetActive();
// CWnd *pWnd = ((CRichFrame*)pMainFrame)->m_wndSplitter.GetPane(0,0);
// CRichSyntaxView *pRich = (CRichSyntaxView*)pWnd;
// pRich->SetFileName(strPath);
// pRich->OnOpen();
/////////////////////////////////////////////////////////////////////
} *pResult = 0;
}////////////////////////////////////////////////////////////////////////
//µ±ÐèÒªÕ¹¿ª½Úµãʱ£¬½«¸Ã½ÚµãϵÄÄÚÈݼÓÈë
////////////////////////////////////////////////////////////////////////
void CFileTreeView::OnItemexpanding(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
CTreeCtrl &tree = m_tree;
HTREEITEM hti = pNMTreeView->itemNew.hItem;
if (hti != tree.GetRootItem())
{
//Çå¿Õ¶ù×Ó
HTREEITEM hChild = tree.GetChildItem(hti);
while (hChild)
{
tree.DeleteItem(hChild);
hChild = tree.GetChildItem(hti);
} char *szCurDir;
HTREEITEM hParent;
CString strPath,str1,str2; strPath = tree.GetItemText(hti);
hParent = tree.GetParentItem(hti);
while (hParent != tree.GetRootItem())
{
strPath = tree.GetItemText(hParent) + "\\" + strPath;
hParent = tree.GetParentItem(hParent);
}
strPath += "\\";
str1 = strPath.Mid(strPath.Find(":") - 2 + 1,2);
str2 = strPath.Right(strPath.GetLength() - strPath.Find(":") - 2);
strPath = str1 + str2; szCurDir = (LPSTR)(LPCSTR)strPath;
if (!GetSubSiblingItem(hti,szCurDir))
HideButton(hti);
} *pResult = 0;
}void CFileTreeView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
if (m_tree)
m_tree.MoveWindow(0,0,cx,cy,TRUE);
}int CFileTreeView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
if (!m_tree)
m_tree.Create(WS_CHILD|WS_VISIBLE|TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS,CRect(0,0,0,0),this,ID_FILETREE); if (!m_ImageList)
m_ImageList.Create(IDB_FILE,16,16,RGB(0,255,0)); InitRootDirectory(); return 0;
}void CFileTreeView::OnFiletreeRefresh()
{
InitRootDirectory();
}
void CMainFrame::OnBrowse()
{
CString str;
BROWSEINFO bi;
char name[MAX_PATH];
ZeroMemory(&bi,sizeof(BROWSEINFO));
bi.hwndOwner=GetSafeHwnd();
bi.pszDisplayName=name;
bi.lpszTitle=_T("Browse Folder");
bi.ulFlags=BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT;
LPITEMIDLIST idl=SHBrowseForFolder(&bi);
if(idl==NULL)
return;
SHGetPathFromIDList(idl,str.GetBuffer(MAX_PATH));
str.ReleaseBuffer();
CString path=str;
if(str.GetAt(str.GetLength()-1)!='\\')
path+="\\";
return path;
}
可以得到路径名了,谢谢你了,怎么让它在EDIT里面显示呢?我把EDIT跟一个变量m_edit绑定了,可是用m_edit=path; UPDATEDATA();无法显示。
然后就是最后一个问题了,怎么把路径下的所有文件的文件名都显示在LIST里面呢
{
static WIN32_FIND_DATA fData;
int OldLength = (int)strlen(path);
HANDLE H;
strcat(path, "*.*");
H = FindFirstFile(path, &fData);
path[OldLength] = '\0';
do{
if(fData.cFileName[0] == '.')
continue;
if(fData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
strcat(path, fData.cFileName);
strcat(path, "\\");
ReadDirectory(path);
path[OldLength] = '\0';
}
else
{
strcat(path, fData.cFileName);
InsertItem(m_nTotal,path, 0);
path[OldLength] = '\0';
}
}while(FindNextFile(H, &fData));
}