// Dirdialog. h
//////////////////////////////////////// ////////////////////////////////
// Dirdialog. h: interface for the cdirdialog class.
//////////////////////////////////////// //////////////////////////////
# If! Defined (afx_dirdialog_h1_62ffac92_1dee_11d1_b87a_0060979cdf6d1_included _)
# Define afx_dirdialog_h1_62ffac92_1dee_11d1_b87a_0060979cdf6d1_encoded _
# If _ msc_ver> = 1000
# Pragma once
# Endif // _ msc_ver >=1000
Class cdirdialog
{
Public:
Cdirdialog ();
Virtual ~ Cdirdialog ();
Bool dobrowse (cwnd * pwndparent = NULL );
Cstring m_strwindowtitle;
Cstring m_strpath;
Cstring m_strinitdir;
Cstring m_strseldir;
Cstring m_strtitle;
Int m_iimageindex;
Bool m_bstatus;
PRIVATE:
Virtual bool selchanged (lpcstr/* lpcsselection */, cstring &/* csstatustext */) {return true ;};
Static int _ stdcall cdirdialog: browsectrlcallback (hwnd, uint umsg, lparam, lparam lpdata );
};
# Endif //! Defined (afx_dirdialog_h1_62ffac92_1dee_11d1_b87a_0060979cdf6d1_included _)
// Dirdialog. cpp
//////////////////////////////////////// ///////////////////////////////////
// Dirdialog. cpp: Implementation of the cdirdialog class.
//////////////////////////////////////// //////////////////////////////
# Include "stdafx. H"
# Include "dirdialog. H"
# Include "shlobj. H"
# Ifdef _ debug
# UNDEF this_file
Static char this_file [] =__ file __;
# Define new debug_new
# Endif
// Callback function called by shbrowseforfolder's browse Control
// After initialization and when selection changes
Int _ stdcall cdirdialog: browsectrlcallback (hwnd, uint umsg, lparam, lparam lpdata)
{
Cdirdialog * pdirdialogobj = (cdirdialog *) lpdata;
If (umsg = bffm_initialized)
{
If (! Pdirdialogobj-> m_strseldir.isempty ())
: Sendmessage (hwnd, bffm_setselection, true, (lparam) (lpctstr) (pdirdialogobj-> m_strseldir ));
If (! Pdirdialogobj-> m_strwindowtitle.isempty ())
: Setwindowtext (hwnd, (lpctstr) pdirdialogobj-> m_strwindowtitle );
}
Else if (umsg = bffm_selchanged)
{
Lpitemidlist pidl = (lpitemidlist) lparam;
Char Selection [max_path];
If (! : Shgetpathfromidlist (pidl, selection ))
Selection [0] = '\ 0 ';
Cstring csstatustext;
Bool Bok = pdirdialogobj-> selchanged (selection, csstatustext );
If (pdirdialogobj-> m_bstatus)
: Sendmessage (hwnd, bffm_setstatustext, 0, (lparam) (lpcstr) csstatustext );
: Sendmessage (hwnd, bffm_enableok, 0, Bok );
}
Return 0;
}
//////////////////////////////////////// //////////////////////////////
// Construction/destruction
//////////////////////////////////////// //////////////////////////////
Cdirdialog: cdirdialog ()
{
M_bstatus = false;
}
Cdirdialog ::~ Cdirdialog ()
{
}
Bool cdirdialog: dobrowse (cwnd * pwndparent)
{
If (! M_strseldir.isempty ())
{
M_strseldir.trimright ();
If (m_strseldir.right (1) = "\" | m_strseldir.right (1) = "//")
M_strseldir = m_strseldir.left (m_strseldir.getlength ()-1 );
}
Lpmalloc pmalloc;
If (shgetmalloc (& pmalloc )! = Noerror)
Return false;
Browseinfo binfo;
Lpitemidlist pidl;
Zeromemory (pvoid) & binfo, sizeof (browseinfo ));
If (! M_strinitdir.isempty ())
{
Olechar olepath [max_path];
Ulong cheaten;
Ulong dwattributes;
Hresult hr;
Lpshellfolder p1_topfolder;
//
// Get a pointer to the desktop's ishellfolder interface.
//
If (succeeded (shget?topfolder (& p;topfolder )))
{
//
// Ishellfolder: parsedisplayname requires the file name be in Unicode.
//
Multibytetowidechar (cp_acp, mb_precomposed, m_strinitdir.getbuffer (max_path),-1,
Olepath, max_path );
M_strinitdir.releasebuffer (-1 );
//
// Convert the path to an itemidlist.
//
HR = psf-topfolder-> parsedisplayname (null,
Null,
Olepath,
& Cheaten,
& Pidl,
& Dwattributes );
If (failed (HR ))
{
Pmalloc-> free (pidl );
Pmalloc-> release ();
Return false;
}
Binfo. pidlroot = pidl;
}
}
Binfo. hwndowner = pwndparent = NULL? Null: pwndparent-> getsafehwnd ();
Binfo. pszdisplayname = m_strpath.getbuffer (max_path );
Binfo. lpsztitle = (m_strtitle.isempty ())? "Open": m_strtitle;
Binfo. ulflags = bif_returnfsancestors
| Bif_returnonlyfsdirs
| (M_bstatus? Bif_statustext: 0 );
Binfo. lpfn = browsectrlcallback; // address of Callback Function
Binfo. lparam = (lparam) This; // pass address of object to callback function
If (pidl =: shbrowseforfolder (& binfo) = NULL)
{
Return false;
}
M_strpath.releasebuffer ();
M_iimageindex = binfo. iimage;
If (: shgetpathfromidlist (pidl, m_strpath.getbuffer (max_path) = false)
{
Pmalloc-> free (pidl );
Pmalloc-> release ();
Return false;
}
M_strpath.releasebuffer ();
Pmalloc-> free (pidl );
Pmalloc-> release ();
Return true;
}
// Call
Cdirdialog dlgdir;
Dlgdir. m_strtitle = _ T ("Select Directory for file search ");
Updatedata (true );
Dlgdir. m_strseldir = "C :\\";
Dlgdir. m_strwindowtitle = _ T ("Select Directory ");
If (dlgdir. dobrowse (this) = idok ){
M_edit1 = dlgdir. m_strpath; // write the file path to the editing control.
// Append backslash if necessary
Updatedata (false );
}
// Configure //-------------------------------------------------------------------------------------------------------------------------
// Another method:
Browseinfo Bi = {0 };
Lpitemidlist pidl;
Imalloc * pmalloc = NULL;
Char cszpath [max_path];
Bi. hwndowner = m_hwnd;
Bi. lpsztitle = "title ";
Bi. ulflags = bif_returnonlyfsdirs;
Pidl = shbrowseforfolder (& BI );
If (pidl)
{
If (shgetpathfromidlist (pidl, cszpath ))
{
// Get dir
}
If (shgetmalloc (& pmalloc ))
{
Pmalloc-> free (pidl );
Pmalloc-> release ();
}
}