Win32选择保存文件路径和打开文件夹的函数
2014年9月30日
有时候我们需要拾取打开或者保存的路径,浏览文件夹,下面是win32的接口。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
#include <shlobj.h> #include <windows.h> int GetOpenOrSaveFileDialog(HWND hwnd,TCHAR filename[],int maxlen) { OPENFILENAME ofn; BOOL rc; memset(&ofn,0,sizeof(ofn)); ofn.lStructSize = sizeof(ofn); ofn.hwndOwner = hwnd; ofn.lpstrFile = filename; ofn.nMaxFile = maxlen*sizeof(filename[0]); ofn.nFilterIndex = 1; ofn.Flags = OFN_PATHMUSTEXIST ; ofn.lpstrFilter = TEXT("MPG file(*.mpg)\0*.mpg\0") TEXT("MP4 file(*.mpg)\0*.mp4\0"); rc = GetSaveFileName(&ofn); /*Save As*/ /* GetOpenFileName(&ofn);*//*Open*/ return rc; } int GetOpenFolderDialog(HWND hwnd,TCHAR* folder) { BROWSEINFO bi = {0}; LPITEMIDLIST pIDList; IMalloc * imalloc = 0; bi.hwndOwner = hwnd; bi.lpszTitle = TEXT("select a folder"); bi.ulFlags = BIF_RETURNONLYFSDIRS|BIF_EDITBOX; pIDList = SHBrowseForFolder(&bi); if(pIDList) { SHGetPathFromIDList(pIDList,folder); if (SUCCEEDED(SHGetMalloc(&imalloc))) { imalloc->lpVtbl->Free (imalloc,pIDList); imalloc->lpVtbl->Release(imalloc); return TRUE; } } return FALSE; } |
MSDN:GetSaveFileName
MSDN:SHBrowseForFolder
分类: Win32