Standarddialog - Aufruf des Ordnerauswahl-Dialogs



  • Wie rufe ich den Standard Windows-Dialog auf, mit dem ich Verzeichnisse wählen kann. Sowas ähnliches kommt bei jeden Installprog und sowas bräuchte ich auch.
    Quelltext, Vorschläge und dumme Kommentare sind erwünscht. Aber am liebsten Quelltext

    [ Dieser Beitrag wurde am 03.02.2002 um 18:52 Uhr von dEUs editiert. ]



  • Source ???????

    OK !!!

    Die Headerdatei

    ////////////////////////////////////////////////////////////////////////
    // DirDialog.h: interface for the CDirDialog class.
    //////////////////////////////////////////////////////////////////////
    
    #if !defined(AFX_DIRDIALOG_H__62FFAC92_1DEE_11D1_B87A_0060979CDF6D__INCLUDED_)
    #define AFX_DIRDIALOG_H__62FFAC92_1DEE_11D1_B87A_0060979CDF6D__INCLUDED_
    
    #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 hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData);
    };
    
    #endif // !defined(AFX_DIRDIALOG_H__62FFAC92_1DEE_11D1_B87A_0060979CDF6D__INCLUDED_)
    

    Die Cpp-Datei

    #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 hwnd, UINT uMsg, LPARAM 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 pDesktopFolder;
           //
           // Get a pointer to the Desktop's IShellFolder interface.
           //
           if (SUCCEEDED(SHGetDesktopFolder(&pDesktopFolder)))
           {
               //
               // 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 = pDesktopFolder->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;
    }
    

    Die Implementierung

    void CFiletestDlg::OnButton3()
    {
       // TODO: Code für die Behandlungsroutine der Steuerelement-Benachrichtigung hier einfügen
          UpdateData(TRUE);
    
       CDirDialog dlg;
       dlg.m_strTitle = _T("Choose directory");
       dlg.m_strSelDir = m_files;
    
       if (dlg.DoBrowse())
       {
           m_dir = dlg.m_strPath + "\\";
    
           //m_combo2.SetEditSel(m_files.GetLength(), m_files.GetLength());
       }
    CString m_dirund = m_dir + "*.*";
    m_dirwahl = m_dir;
    m_list1.ResetContent();
    
    m_list1.Dir(DDL_READONLY,m_dirund);
    m_anzahlfiles=m_list1.GetCount();
       UpdateData(FALSE);
    
    }
    

    Ein Filedialog:

    void CFiletestDlg::OnButton4()
    {
       // TODO: Code für die Behandlungsroutine der Steuerelement-Benachrichtigung hier einfügen
      UpdateData(TRUE);
    
       //CString strFilter = _T("All Files (*.*) |*.*| |");
       CString strFilter = _T("INI (*.INI) |*.INI| |");
    
       CFileDialog    dlg(TRUE, NULL, m_files, OFN_HIDEREADONLY|OFN_EXPLORER, strFilter, NULL);
       dlg.m_ofn.lpstrTitle = _T("File auswählen");
    
       if (dlg.DoModal() == IDOK)
       {
           m_dir = dlg.GetPathName();
           UpdateData(FALSE);
           //m_combo2.SetEditSel(dlg.GetPathName().GetLength(), dlg.GetPathName().GetLength());
       }    
    }
    

    In der Implementierung mußt Du dir noch das für Dich benötigte raussuchen sonst ist die Klasse funktionierend (x.h;x.cpp).
    Ist von einem meiner Programme.

    MFG

    Thomas


Anmelden zum Antworten