-
API::Browse Dialog Box
I Was wondering if someone could point me in the right direction:
I am looking for a common dialog box that allows the user to browse and select the directory that the user desires. I am looking for the directory and not a filename, so GetOpenFileName() would not be the correct API.
I tried searching msdn to no avail because the toc didn't feel like cooperating.
Thanks in advance,
~bc17
-
How about SHBrowseForFolder()?
-
This is old code and comes with no warranty.
Code:
LONG WINAPI BrowseCallbackProc(HWND hwnd,UINT uMsg, LPARAM lParam, LPARAM lpData) {
CHAR tempStr[MAX_PATH];
// If the BFFM_INITIALIZEDemp message is received
// set the path to the current setting
switch (uMsg) {
case BFFM_INITIALIZED:
GetDlgItemText( (HWND) lpData,CNTRL_FOLDER,tempStr,MAX_PATH);
// Send a message to the dialog box telling it to select this path.
SendMessage(hwnd, BFFM_SETSELECTIONA, TRUE, (LPARAM) tempStr);
}
return 0; // the function should always return 0
}
void BrowseForFolder(HWND hwnd) {
BROWSEINFO bi; // structure passed to the function
LPITEMIDLIST pidl; // PIDL to the user's selection
CHAR tempStr[MAX_PATH]; // string used to temporarily hold the physical path
LONG retval; // return value
// Initialize the structure to be passed to the function.
ZeroMemory(&bi, sizeof(bi) );
bi.hwndOwner = hwnd;
bi.pszDisplayName = tempStr;
bi.lpszTitle = "Please choose a folder."; // Message displayed to the user
bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE;
bi.lpfn = BrowseCallbackProc;
bi.lParam = (LONG) hwnd;
pidl = SHBrowseForFolder(&bi);
if (pidl != 0) {
//get folder location
retval = SHGetPathFromIDList(pidl, tempStr);
//Free the pidl
CoTaskMemFree(pidl);
if (retval == TRUE) {
SetDlgItemText(hwnd,CNTRL_FOLDER,tempStr);
return;
}
}
return;
}
-
Thx
Thx for the reply. I managed to get my prog working using the source code as a guide.
Thx,
~bc17
-
You also could use this:
Code:
/* Displays a dirlistbox */
int GetFolder(HWND hWnd, char *achBuf, char *achTitle){
LPITEMIDLIST lpItemList=0;
BROWSEINFO bi;
LPMALLOC lpM;
char chRet(0);
memset(&bi, 0, sizeof(BROWSEINFO));
if(!SHGetMalloc (&lpM)){
bi.hwndOwner = hWnd;
bi.pszDisplayName = achTitle;
bi.pidlRoot = NULL;
bi.lpszTitle = "Velg folder";
bi.ulFlags=BIF_RETURNONLYFSDIRS;
if((lpItemList = SHBrowseForFolder(&bi))){
SHGetPathFromIDList(lpItemList,achBuf);
chRet=1;
}else
chRet=0;
lpM->Free(lpItemList);
lpM->Release();
}else
chRet=0;
return(chRet);
}