重新包裝CFileDialog

通常,我們呼叫FileDialog,要嘛就是選取多檔,要嘛就是選取一檔。這個限制設計,是需要的,不然可能會造成選檔結果的錯誤。

為了簡化整個過程,在此重新包裝了整個類別的細部。
來看程式碼吧!

選取單一檔案的CFileDialogSingle

#ifndef CFILEDLGSINGLE_H
#define CFILEDLGSINGLE_H

enum FDlgType{SAVEAS = 0, OPEN = 1};
#include <vector>

class CFileDlgSingle
{
    CFileDialog* m_pFileDlg;
    FDlgType m_isOpen;
    std::vector<CString> m_filters;
    CString getFilters();

public:
    CFileDlgSingle(FDlgType isOpen);
    ~CFileDlgSingle();
    //CFileDlgSingle()

    void AddFileFilter(CString descript, CString exName);

    int DoModal();
    int Show(){ return DoModal(); }

    CString GetFileExt() const
    { return m_pFileDlg->GetFileExt(); }

    CString GetFileName() const
    { return m_pFileDlg->GetFileName(); }

    CString GetFileTitle() const
    { return m_pFileDlg->GetFileTitle(); }

    CString GetFolderPath() const
    { return m_pFileDlg->GetFolderPath(); }

    CString GetPathName() const
    { return m_pFileDlg->GetPathName(); }
};


#endif
#include "stdafx.h"
#include "FileDlgSingle.h"
#include <iterator>

CFileDlgSingle::CFileDlgSingle(FDlgType isOpen) :m_isOpen(isOpen){ }
CFileDlgSingle::~CFileDlgSingle(){ delete m_pFileDlg; }

CString CFileDlgSingle::getFilters()
{
    CString totalFilters;
    for (std::vector<CString>::iterator it = m_filters.begin(); it != m_filters.end(); ++it)
    {
        totalFilters += *it + "|";
    }
    return totalFilters + "|";
}

void CFileDlgSingle::AddFileFilter(CString descript, CString exName)
{
    CString oneFilter;
    oneFilter = descript + "(*." + exName + ")|*." + exName;
    m_filters.push_back(oneFilter);
}

int CFileDlgSingle::DoModal()
{
    m_pFileDlg = new CFileDialog(m_isOpen, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, getFilters(), NULL);
    return m_pFileDlg->DoModal();
}
選取多個檔案的CFileDialogMulti
#ifndef CFILEDLGSINGLE_H
#define CFILEDLGSINGLE_H

enum FDlgType{SAVEAS = 0, OPEN = 1};
#include <vector>

class CFileDlgMulti
{
    CFileDialog* m_pFileDlg;
    FDlgType m_isOpen;
    std::vector<CString> m_filters;
    CString getFilters();

    CString m_szFileNameBuffer;
public:
    CFileDlgMulti(FDlgType isOpen);
    ~CFileDlgMulti();
    //CFileDlgSingle()

    void AddFileFilter(CString descript, CString exName);

    int DoModal();
    int Show(){ return DoModal(); }

    CString GetFileExt() const
    { return m_pFileDlg->GetFileExt(); }

    CString GetFileName() const
    { return m_pFileDlg->GetFileName(); }

    CString GetFileTitle() const
    { return m_pFileDlg->GetFileTitle(); }

    CString GetFolderPath() const
    { return m_pFileDlg->GetFolderPath(); }

    CString GetPathName() const
    { return m_pFileDlg->GetPathName(); }

    const int GetSelFileList(std::vector<CString>& vstrFilePath);
    void SetSelMultiFileTotal(const int& FileMaxBuffer);
};

#endif
#include "stdafx.h"
#include "FileDlgMulti.h"
#include <iterator>

CFileDlgMulti::CFileDlgMulti(FDlgType isOpen) :m_isOpen(isOpen){ }
CFileDlgMulti::~CFileDlgMulti(){ delete m_pFileDlg; }

CString CFileDlgMulti::getFilters()
{
    CString totalFilters;
    for (std::vector<CString>::iterator it = m_filters.begin(); it != m_filters.end(); ++it)
    {
        totalFilters += *it + "|";
    }
    return totalFilters + "|";
}

void CFileDlgMulti::AddFileFilter(CString descript, CString exName)
{
    CString oneFilter;
    oneFilter = descript + "(*." + exName + ")|*." + exName;
    m_filters.push_back(oneFilter);
}

int CFileDlgMulti::DoModal()
{
    m_pFileDlg = new CFileDialog(m_isOpen, NULL, NULL, OFN_ALLOWMULTISELECT | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, getFilters(), NULL);
    return m_pFileDlg->DoModal();
}

const int CFileDlgMulti::GetSelFileList(std::vector>CString>& vstrFilePath)
{
    // 取得第一個檔案名稱的位置 若沒有的話傳回NULL
    POSITION pos = m_pFileDlg->GetStartPosition();
    if (pos) vstrFilePath.clear();
    while(pos != NULL)
        vstrFilePath.push_back(m_pFileDlg->GetNextPathName(pos));// 透過位置來取得檔案名稱 檔案名稱包含完整路徑
    return (int)vstrFilePath.size();
}

void CFileDlgMulti::SetSelMultiFileTotal(const int& FileMaxBuffer)
{
    const int BufferSize = (FileMaxBuffer * (MAX_PATH + 1)) + 1;
    m_pFileDlg->m_ofn.nMaxFile = BufferSize;
    m_pFileDlg->m_ofn.lpstrFile = m_szFileNameBuffer.GetBuffer(BufferSize);
    m_szFileNameBuffer.ReleaseBuffer();
    m_pFileDlg->m_ofn.lpstrFile[0] = NULL;
}
這兩組程式碼其實可以提取共同的部份做為抽象類別。
有空再來試試吧!^^
最重要的就是,使用端的程式碼會有多簡單。
CFileDlgSingle dlgFile(OPEN);
dlgFile.AddFileFilter("純文字檔", "txt");
dlgFile.AddFileFilter("All File", "*");
dlgFile.Show();

m_staticText.SetWindowText(dlgFile.GetPathName());

沒有留言:

張貼留言

(什麼是留言欄訊息?)