最近转做mobile 下的开发,PC里浏览文件夹我们一般用MFC的CFildDialog类来实现,或者调用SHBrowseForFolder() 也可以,可是在mobile 里CFildDialog 和 SHBrowseForFolder 都没办法用,不会叫我写个文件浏览的树吧,好麻烦 SHBrowseForFolder 是支持win32 wince 但不支持 WM ,无奈只好继续找,找了许久终于在英文网站上找到一个帖子 说可以用GetOpenFileName() 来代替.msdn 里也有这个方法的例子.
Code Example
The following code example demonstrates how to use GetOpenFileNameEx.
Note To make the following code example easier to read, security checking and error handling are not included. This code example should not be used in a release configuration unless it has been modified to include them.
BOOL UseFilePicker(HWND hwndOwner)
{
TCHAR szFile[MAX_PATH];
OPENFILENAMEEX ofnex = {0};
ofnex.lStructSize = sizeof(ofnex);
ofnex.hwndOwner = hwndOwner;
ofnex.lpstrFile = szFile;
ofnex.nMaxFile = sizeof(szFile)/sizeof(szFile[0]);
ofnex.lpstrFilter = TEXT("All Files (*.*)/0*.*/0");
ofnex.lpstrTitle = TEXT("Thumbnail View");
// Show thumbnails of files that are not DRM protected
ofnex.ExFlags = OFN_EXFLAG_THUMBNAILVIEW | OFN_EXFLAG_HIDEDRMPROTECTED;
ofnex.lpstrInitialDir = NULL;
return GetOpenFileNameEx(&ofnex);
}
Code Example
The following code example displays a dialog in which the user may select only files of type *.BMP. The function returns TRUE if the user selects a file, and the buffer is set to the full path and name of the selected file. If the function returns FALSE, the contents of the buffer are undefined.
Note To make the following code example easier to read, security checking and error handling are not included. This code example should not be used in a release configuration unless it has been modified to include them.
BOOL SelectBMP(LPTSTR pszFilename, DWORD cchBufSize)
{
ASSERT(pszFilename);
OPENFILENAMEEX ofn = {0};
ofn.lStructSize = sizeof(ofn);
ofn.lpstrFilter = _T("BMP Files (*.bmp)/0*.bmp/0");
ofn.lpstrFile = pszFilename;
ofn.nMaxFile = cchBufSize;
ofn.lpstrInitialDir = _T("//Program Files//MyApp");
ofn.lpstrTitle = _T("Select a bitmap");
ofn.ExFlags = OFN_EXFLAG_THUMBNAILVIEW;
return GetOpenFileNameEx(&ofn));
}
==================
void CBrowseFileDlg::OnBnClickedBrowse()
{
// TODO: 在此添加控件通知处理程序代码
OPENFILENAMEEX ofn = {0};
TCHAR szFile[MAX_PATH];
ofn.lStructSize = sizeof(ofn);
ofn.lpstrFilter = _T("BMP Files (*.bmp)/0*.bmp/0");
ofn.lpstrFile = szFile;
ofn.nMaxFile = sizeof(szFile)/sizeof(szFile[0]);
ofn.lpstrInitialDir = _T("//Program Files"); // 这里可以指定打开的默认目录
ofn.lpstrTitle = _T("Select a bitmap");
ofn.ExFlags = OFN_EXFLAG_THUMBNAILVIEW | OFN_EXFLAG_HIDEDRMPROTECTED;
GetOpenFileNameEx(&ofn);
CString strFileName = ofn.lpstrFile;
MessageBox(strFileName,L"选择的文件");
}