In some environments (such as Win PE), Windows comes with a Select folder API that cannot be used or is poorly experienced. The selection of folder selection function is now realized by using the GetOpenFileName parameter.
#include <Windows.h>#include<CommCtrl.h>#include<locale.h>#include<shlwapi.h>LONG G_loriwndproc=NULL;#defineId_combo_addr 0x47c#defineId_left_toobar 0x4a0LRESULTStatic__stdcall _wndproc (HWND hwnd, UINT umsg, WPARAM WPARAM, LPARAM LPARAM) {Switch(umsg) { Casewm_command: {if(WParam = =IDOK) {wchar_t Wcdirpath[max_path]= {0}; HWND hcomboaddr=GetDlgItem (hwnd, ID_COMBO_ADDR); if(Hcomboaddr! =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) {intSize = 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 a wm_commond message from the left toolbar (that is, click on the left toolbar), clear the combo box next to the OK button. HWND Hctrl =(HWND) LParam; if(Hctrl = =NULL) { Break; } intCtrlId =GetDlgCtrlID (Hctrl); if(CtrlId = =Id_left_toobar) {HWND hcomboaddr=GetDlgItem (hwnd, ID_COMBO_ADDR); if(Hcomboaddr! =NULL) {SETWINDOWTEXTW (hcomboaddr, L""); } } } Break; } inti =CallWindowProc ((WNDPROC) G_loriwndproc, hwnd, umsg, WParam, LParam); returni;} Uint_ptrStatic__stdcall Myfolderproc (HWND hdlg, UINT uimsg, WPARAM WPARAM, LPARAM LPARAM) {//refer to ReactOS, Hdlg is a hidden dialog box whose parent window is processed in the parent window for the Open file dialog, Ok,cancel button, and other controls. if(Uimsg = =wm_notify) {lpofnotify lpofnotify=(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 (hdlg), Wcdirpath,sizeof(Wcdirpath)); HWND hcomboaddr=GetDlgItem (GetParent (hdlg), id_combo_addr); if(Hcomboaddr! =NULL) { if(Wcslen (Wcdirpath)) {//Remove the suffix name for the folder shortcut. intPathsize =wcslen (Wcdirpath); if(Pathsize >=4) {wchar_t* Wcextension =pathfindextensionw (Wcdirpath); if(Wcslen (wcextension)) {wcextension=charlowerw (wcextension); if(!WCSCMP (Wcextension, L". lnk") ) {wcdirpath[pathsize-4] = L' /'; }}} setwindowtextw (Hcomboaddr, Wcdirpath); } Else{setwindowtextw (hcomboaddr, L""); } } } } return 1;}int_tmain (intARGC, _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
GetOpenFileName How to select a folder