GetOpenFileName: How to Select a folder, getopenfilename
In some environments (such as win pe), the selection folder api provided by windows cannot be used or the user experience is poor. Now, the GetOpenFileName return parameter is used to select a folder.
# Include <Windows. h> # include <CommCtrl. h> # include <locale. h> # include <shlwapi. h> LONG g_lOriWndProc = NULL; # define returns 0x47c # define returns 0x4A0LRESULT static _ stdcall _ WndProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {switch (uMsg) {case WM_COMMAND: {if (wParam = IDOK) {wchar_t wcDirPath [MAX_PATH] = {0}; HWND hComboAddr = GetDlgItem (hwnd, ID_COMBO_ADDR); if (hCo MboAddr! = NULL) {GetWindowText (hComboAddr, wcDirPath, MAX_PATH);} if (! Wcslen (wcDirPath) {break;} DWORD dwAttr = GetFileAttributes (wcDirPath); if (dwAttr! =-1 & (FILE_ATTRIBUTE_DIRECTORY & dwAttr) {LPOPENFILENAMEW oFn = (LPOPENFILENAME) GetProp (hwnd, L "OPENFILENAME"); if (oFn) {int size = oFn-> nMaxFile> MAX_PATH? MAX_PATH: oFn-> nMaxFile; memcpy (oFn-> lpstrFile, wcDirPath, size * sizeof (wchar_t); RemoveProp (hwnd, L "OPENFILENAME"); EndDialog (hwnd, 1) ;}else {EndDialog (hwnd, 0) ;}} break ;} //////////////////////////////////////// /// // if it is the left toolbar WM_COMMOND message (click the toolbar on the left ), then, the combo box next to the OK button is cleared. HWND hCtrl = (HWND) lParam; if (hCtrl = NULL) {break;} int ctrlId = GetDlgCtrlID (hCtrl); if (ctrlId = ID_LEFT_TOOBAR) {HWND hComboAddr = GetDlgItem (hwnd, ID_COMBO_ADDR); if (hComboAddr! = NULL) {SetWindowTextW (hComboAddr, L "") ;}} break;} int I = CallWindowProc (WNDPROC) g_lOriWndProc, hwnd, uMsg, wParam, lParam ); return I;} UINT_PTR static _ stdcall MyFolderProc (HWND hdlg, UINT uiMsg, WPARAM wParam, LPARAM lParam) {// refer to reactos, hdlg is a hidden dialog box, the parent window is the dialog box for opening files, and the OK, CANCEL buttons and other controls are used to process messages in the parent window. If (uiMsg = WM_NOTIFY) {LPOFNOTIFY lpofy y = (lpOfNotify) lParam; if (LPOFNOTIFY-> hdr. code = CDN_INITDONE) {SetPropW (GetParent (hdlg), L "OPENFILENAME", (HANDLE) (lpOfNotify-> lpOFN); g_lOriWndProc =: SetWindowLongW (:: getParent (hdlg), GWL_WNDPROC, (LONG) _ WndProc);} if (lpOfNotify-> hdr. code = CDN_SELCHANGE) {wchar_t wcDirPath [MAX_PATH] = {0}; CommDlg_OpenSave_GetFilePathW (GetParent (hdl G), wcDirPath, sizeof (wcDirPath); HWND hComboAddr = GetDlgItem (GetParent (hdlg), ID_COMBO_ADDR); if (hComboAddr! = NULL) {if (wcslen (wcDirPath) {// remove the suffix of the folder shortcut. Int pathSize = wcslen (inflow); if (pathSize> = 4) {wchar_t * wcExtension = inflow (wcDirPath); if (wcslen (wcExtension) {wcExtension = CharLowerW (wcExtension ); if (! Wcscmp (wcExtension, L ". lnk ") {wcDirPath [pathSize-4] = L' \ 0' ;}} SetWindowTextW (hComboAddr, wcDirPath);} else {SetWindowTextW (hComboAddr, L "") ;}}}return 1;} int _ tmain (int argc, _ TCHAR * argv []) {setlocale (LC_ALL, "chs"); OPENFILENAMEW openFile; memset (& openFile, 0, sizeof (openFile); openFile. lStructSize = sizeof (openFile); wchar_t szFileName [MAX_PATH] = {0}; OPENFILENAME openFileName = {0}; openFile. lStructSize = sizeof (OPENFILENAME); openFile. nMaxFile = MAX_PATH; openFile. lpstrFilter = L "Folder \ 0 .. \ 0 \ 0 "; openFile. lpstrFile = szFileName; openFile. nFilterIndex = 1; openFile. flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_ENABLEHOOK | OFN_HIDEREADONLY; openFile. hInstance = (HMODULE) GetCurrentProcess (); openFile. lpfnHook = MyFolderProc; BOOL B = GetOpenFileNameW (& openFile); wprintf (L "% d % s \ n", B, szFileName); system ("pause "); return 0 ;}
Reference: http://blog.csdn.net/norsd/article/details/3476606