Click here to Skip to main content
15,897,518 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi,

I have an MFC application which uses "CDirDialog" to open directory dialog.

On WindowsXP it works fine, it opens a directory dialog but on Windows7 it is opening a File dialog and not a directory dialog at all.

Does anybody know why this is happening and what shall we do to solve this issue ?

Here is a sample code :
C#
CDirDialog  GetDirDlg(AfxGetMainWnd());
GetDirDlg.m_ofn.lpstrTitle = _T( "Choose Project Top Directory" );
    
if (GetDirDlg.DoModal() == IDOK)
{
m_sTopPath = GetDirDlg.m_sDirectory;
}

On Win7 it is opening file dialog whereas it is expected to open a directory dialog.

Thanks in advance.

- Prasad.
Posted
Updated 21-Jun-12 0:30am
v2
Comments
Jochen Arndt 21-Jun-12 6:38am    
CDirDialog is not a MFC class. So you should supply more information on the used code (at least provide a link to the source of the CDirDialog implementation).

Hi,

CDirDialog is not a part of MFC, you can use any of the below options


C++
CFolderPickerDialog  fFolder;
if( fFolder.DoModal() == IDOK  )
{
    CString strFolderPath  = fFolder.GetFolderPath();
}


or
CFolderDialog-Selecting-Folders
[^]

or
SHBrowseForFolder function
http://msdn.microsoft.com/en-us/library/windows/desktop/bb762115%28v=vs.85%29.aspx[^]
 
Share this answer
 
Comments
prasad@codeproj 25-Jun-12 6:13am    
Hi,

Thank you very much, SHBrowseForFolder() solved my problem.
CDirDialog I was using was actually inheriting CFileDialog.
class CDirDialog : public CFileDialog
{
It is able to open a directory dialog in WinXP but on Win7 it is not.
Do you have any idea why it does not work on Win7 ? please let me know.
Thanks once again!

- Prasad.
Binu MD 25-Jun-12 20:57pm    
Hi Prasad,

I think it is possible, please check this http://www.codeproject.com/Articles/9865/XFolderDialog-a-folder-selection-dialog-based-on-C
prasad@codeproj 28-Jun-12 22:59pm    
Hi Binu,

Thanks a lot ! it works.
To solve my problem what I had to do is only override a DoModal() function and add few lines in it -

int CDirDialog::DoModal()
{
m_ofn.hwndOwner = PreModal();
int nResult = ::GetOpenFileName(&m_ofn);
PostModal();

return nResult ? nResult : IDCANCEL;
}

Thanks once again !

- Prasad.
Binu MD 28-Jun-12 23:26pm    
you welcome :)
With the help of following given code I can now open a dialog to browse folders.

Now I am using ::SHBrowseForFolder() and it works fine !

class CFolderDialog : public CCommonDialog

{
public:
CString m_sPath;
BROWSEINFO m_bi;
CFolderDialog(CWnd* pParentWnd);
static INT CALLBACK BrowseCallbackProc( IN HWND hWnd, IN UINT uMsg, IN LPARAM lParam, IN LPARAM lpData );
int DoModal();
};

//

CFolderDialog::CFolderDialog(CWnd* pParentWnd) : CCommonDialog(pParentWnd)
{
LPCTSTR pszTitle = _T( "Select the root folder for the browse dialog:" );
UINT uFlags = BIF_RETURNONLYFSDIRS | BIF_USENEWUI;
m_bi.hwndOwner = pParentWnd->GetSafeHwnd();
m_bi.pidlRoot = NULL;
m_bi.lpszTitle = pszTitle;
m_bi.ulFlags = uFlags;
m_bi.lpfn = (BFFCALLBACK)&BrowseCallbackProc;
m_bi.lParam = (LPARAM)this;

m_bi.pszDisplayName = new TCHAR[ MAX_PATH ];
ASSERT( m_bi.pszDisplayName != NULL );
}

int CFolderDialog::DoModal()
{
INT_PTR nRet = -1;
m_bi.hwndOwner = PreModal();

LPITEMIDLIST pItemIDList = ::SHBrowseForFolder( &m_bi );
if( pItemIDList )
{
TCHAR szFolPath[ MAX_PATH ];
if( ::SHGetPathFromIDList( pItemIDList, szFolPath ) )
nRet = IDOK;

m_sPath = CString(szFolPath);
}
else
nRet = IDCANCEL;


PostModal();
return ( nRet );
}

INT CALLBACK CFolderDialog::BrowseCallbackProc( IN HWND hWnd,
IN UINT uMsg,
IN LPARAM lParam,
IN LPARAM lpData )
{
INT nRet = 1;
return nRet;
}

// using CFolderDialog -

CFolderDialog dlg(AfxGetMainWnd());
if(dlg.DoModal() == IDOK)
{
m_sInstallPath = dlg.m_sPath;

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900