在MusicPlayer里折腾了很久的文件夹选择对话框代码,终于在SDK的帮助下完成了。贴下代码:

void SelectFolder(HWND hwnd)
{
 LPMALLOC lpMalloc = NULL;
 if(SUCCEEDED(SHGetMalloc(&lpMalloc)))
 {
  TCHAR szPath[MAX_PATH];
  BROWSEINFO bi;
  LPITEMIDLIST pidl = NULL;
  ZeroMemory(szPath, sizeof(szPath));//注意结构体的置零
  ZeroMemory(&bi, sizeof(bi));
  bi.hwndOwner = hwnd;
  bi.pidlRoot = NULL;
  bi.pszDisplayName = szPath;
  bi.lpszTitle = TEXT("选择目录:");
  bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_BROWSEFORCOMPUTER;
  bi.lpfn = NULL;
  bi.lParam = 0;
  bi.iImage = 0;
  CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);//初始化COM库
  if((pidl = SHBrowseForFolder(&bi)) != NULL)//显示文件夹选择对话框并操作
  {
   SHGetPathFromIDList(pidl, szPath);//转换文件夹路径
   FindAllFile(hwnd, szPath, "*.mp3");//查找文件夹下mp3文件
   FindAllFile(hwnd, szPath, "*.wma");//wma
   FindAllFile(hwnd, szPath, "*.wav");//wav
   //MessageBox(NULL, szPath, "", MB_OK);
  }
  //CoTaskMemFree(pidl);
  lpMalloc->Free(pidl);
  lpMalloc->Release();
  CoUninitialize();//关闭COM库
 }
}