Thread: API::Browse Dialog Box

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    30

    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

  2. #2
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    How about SHBrowseForFolder()?

  3. #3
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    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;
    }

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    30

    Thx

    Thx for the reply. I managed to get my prog working using the source code as a guide.

    Thx,
    ~bc17

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    329
    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);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 02-13-2008, 02:59 PM
  2. Parent of a BrowseForFolder dialog box
    By @nthony in forum Windows Programming
    Replies: 4
    Last Post: 01-08-2007, 02:54 PM
  3. New Theme
    By XSquared in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 04-01-2004, 08:00 PM
  4. Dialog Box & Property Sheet :: MFC
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 08-01-2002, 01:33 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM