From: Daum on
My dialog box has a combo box that gets a path string from key board input.

Also, the dialog box has a push button that pop-ups browsing folder dialog
so that users can select a path using a mouse instead of key board input.

Q) How to make pop-up the browsing folders? I believe the "browsing folder"
dialog is already built in by MFC.

Q) Also, how to get the user selection from the pop-ups browsing folders?

Thank you so much for the help.


- Daum
From: Daum on
I used the way below which are from internet and it works.

But I don't know if it is the best way.

Thanks

-Daum

#include "shlobj.h"
#include <string>

bool GetFolder(std::string& folderpath,
const char* szCaption = NULL,
HWND hOwner = NULL)
{
bool retVal = false;

// The BROWSEINFO struct tells the shell
// how it should display the dialog.
BROWSEINFO bi;
memset(&bi, 0, sizeof(bi));

bi.ulFlags = BIF_USENEWUI;
bi.hwndOwner = hOwner;
bi.lpszTitle = szCaption;

// must call this if using BIF_USENEWUI
::OleInitialize(NULL);

// Show the dialog and get the itemIDList for the
// selected folder.
LPITEMIDLIST pIDL = ::SHBrowseForFolder(&bi);

if(pIDL != NULL)
{
// Create a buffer to store the path, then
// get the path.
char buffer[_MAX_PATH] = {'\0'};
if(::SHGetPathFromIDList(pIDL, buffer) != 0)
{
// Set the string value.
folderpath = buffer;
retVal = true;
}

// free the item id list
CoTaskMemFree(pIDL);
}

::OleUninitialize();

return retVal;
}
/////////////////////////////////////////////////////////////////////////////
// CDialogCreateVolume message handlers
void CDialogCreateVolume::OnCreatVolumePath()
{

CString folderPath;
std::string szPath("");

if (GetFolder(szPath, "Select a folder.") == true)
{
folderPath = CString (szPath.c_str());
MessageBox(folderPath);
}
else
{
MessageBox("No folder selected!\n");
}
}


"Daum" wrote:

> My dialog box has a combo box that gets a path string from key board input.
>
> Also, the dialog box has a push button that pop-ups browsing folder dialog
> so that users can select a path using a mouse instead of key board input.
>
> Q) How to make pop-up the browsing folders? I believe the "browsing folder"
> dialog is already built in by MFC.
>
> Q) Also, how to get the user selection from the pop-ups browsing folders?
>
> Thank you so much for the help.
>
>
> - Daum
From: Woody on
On Sep 19, 5:11 pm, Daum <D...(a)discussions.microsoft.com> wrote:
> Q) How to make pop-up the browsing folders? I believe the "browsing folder"
> dialog is already built in by MFC.
>
>  Q) Also, how to get the user selection from the pop-ups browsing folders?

Use function SHBrowseForFolder. You create a BROWSEINFO structure bi
and initialize it appropriately for your app, then

LPITEMIDLIST pIDList = SHBrowseForFolder(&bi);
if(pIDList != NULL)
{
SHGetPathFromIDList(pIDList, path);
}
will give you the path the user chose.


From: Giovanni Dicanio on
Daum ha scritto:
> I used the way below which are from internet and it works.
>
> But I don't know if it is the best way.
>
> Thanks
>
> -Daum

A problem I see in the code you posted is that it is not Unicode-aware.
The problem is that std::string and 'char' buffers are used, instead the
code should use string and buffers based on TCHAR (which expands to
'char' in ANSI/MBCS builds, and to 'wchar_t' in Unicode builds).

So, your code will fail to compile in Unicode builds (which should be
the standard in these days).


> #include "shlobj.h"
> #include <string>
>
> bool GetFolder(std::string& folderpath,
> const char* szCaption = NULL,
> HWND hOwner = NULL)

The prototype of the function may be corrected to something like this
(using CString instead of 'std::string', and TCHR instead of 'char'):

bool GetFolder( CString & folderpath,
const TCHAR * szCaption = NULL,
HWND hOwner = NULL)

> [...]
> if(pIDL != NULL)
> {
> // Create a buffer to store the path, then
> // get the path.
> char buffer[_MAX_PATH] = {'\0'};

A TCHAR should be used above:

TCHAR buffer[...


> /////////////////////////////////////////////////////////////////////////////
> // CDialogCreateVolume message handlers
> void CDialogCreateVolume::OnCreatVolumePath()
> {
>
> CString folderPath;
> std::string szPath("");
>

Just use CString above (no need for std::string):

CString folderPath;

> if (GetFolder(szPath, "Select a folder.") == true)

Use _T() for string literal decorations (or just store strings in string
table):

if ( GetFolder(folderPath, _T("Select a folder.") )
...


HTH,
Giovanni

From: Daum on
They were the answers for my questions.

Thanks Hackers, Giovanni and Woody.

-Daum

"Giovanni Dicanio" wrote:

"Woody" wrote:

> On Sep 19, 5:11 pm, Daum <D...(a)discussions.microsoft.com> wrote:
> > Q) How to make pop-up the browsing folders? I believe the "browsing folder"
> > dialog is already built in by MFC.
> >
> > Q) Also, how to get the user selection from the pop-ups browsing folders?
>
> Use function SHBrowseForFolder. You create a BROWSEINFO structure bi
> and initialize it appropriately for your app, then
>
> LPITEMIDLIST pIDList = SHBrowseForFolder(&bi);
> if(pIDList != NULL)
> {
> SHGetPathFromIDList(pIDList, path);
> }
> will give you the path the user chose.
>
>
>