假设是对标号为ID_BTN_BRS的响应,那么这个消息响应函数可能会是这样子:
[cpp] view plaincopyprint?
- void CXXClass::OnBottonBrows(void)
- {
- //选择文件夹
- char szSelDir[MAX_PATH+1];
- memset(szSelDir,0,MAX_PATH+1);
- BROWSEINFO bi;
- memset(&bi,0,sizeof(BROWSEINFO));
- bi.hwndOwner=GetSafeHwnd(); //or NULL
- bi.pszDisplayName=NULL;
- bi.lpszTitle="请选择图片所在目录";
- bi.ulFlags=BIF_RETURNONLYFSDIRS; /or ...
- LPITEMIDLIST ItemID;
- ItemID=SHBrowseForFolder(&bi);
- if(ItemID)
- {
- SHGetPathFromIDList(ItemID,szSelDir);
- GlobalFree(ItemID); !!!
- }
- else return;
- CString strDir;
- strDir=szSelDir;
- ///寻找选定文件夹下的图片文件
- strNameList.clear(); // 已定义vector<CString> strNameList;
- WIN32_FIND_DATA fd;
- HANDLE hf;
- hf=FindFirstFile(strDir+"\\*.*",&fd);
- if(hf==INVALID_HANDLE_VALUE)
- {
- //.....
- return;
- }
- do
- {
- //非文件夹并且是合法的文件类型(根据扩展名判断其类型)
- if((!(fd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY))&&
- xIsSuppImageType(fd.cFileName))
- strNameList.push_back(strDir+"\\"+fd.cFileName);
- }while(FindNextFile(hf,&fd));
- FindClose(hf);
- nNum=strNameList.size();
- if(nNum==0)
- {
- //....
- return;
- }
- //...
- }
如果只是打开或者浏览文件,比如打开avi格式视频文件,可以
[cpp] view plaincopyprint?
- char *f="视频文件(合适尺寸,低帧率,avi格式)|*avi||";
- CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT|OFN_FILEMUSTEXIST,f,NULL,0,TRUE);
- dlg.m_ofn.lpstrTitle="打开视频";