一图胜千言:
保存了上次使用的记录
浏览切换填写的路径目录
酱紫写的:
CString pathName; SelectPathDlg pathDlg; if (pathDlg.DoModal() == IDCANCEL) { return; } else { CString pathName = pathDlg.getFilePath(); }
#pragma once #include "resource.h" // SelectPathDlg 对话框 class SelectPathDlg : public CDialogEx { DECLARE_DYNAMIC(SelectPathDlg) public: SelectPathDlg(CWnd* pParent = NULL); // 标准构造函数 virtual ~SelectPathDlg(); // 对话框数据 enum { IDD = IDD_FILE_PATH_DLG }; protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持 DECLARE_MESSAGE_MAP() //UI public: afx_msg void OnBnClickedFindPath(); afx_msg void OnBnClickedOk(); afx_msg void OnBnClickedCancel(); virtual BOOL OnInitDialog(); //logic public: void loadSettings(); //加载配置 void saveSettings(); //保存配置 CString getFilePath(); //获得当前结果目录 protected: CString openFilePathDlg(); //返回用户选择的目录 private: CString m_filePath; //目录 };
// SelectPathDlg.cpp : 实现文件 // #include "stdafx.h" #include "SelectPathDlg.h" #include "afxdialogex.h" #include "HGTools.h" #include "XmlHandlePlus.h" // SelectPathDlg 对话框 IMPLEMENT_DYNAMIC(SelectPathDlg, CDialogEx) SelectPathDlg::SelectPathDlg(CWnd* pParent /*=NULL*/) : CDialogEx(SelectPathDlg::IDD, pParent) , m_filePath(_T("")) { } SelectPathDlg::~SelectPathDlg() { } void SelectPathDlg::DoDataExchange(CDataExchange* pDX) { CDialogEx::DoDataExchange(pDX); DDX_Text(pDX, IDC_PATH_EDITER, m_filePath); } BEGIN_MESSAGE_MAP(SelectPathDlg, CDialogEx) ON_BN_CLICKED(ID_FIND_PATH, &SelectPathDlg::OnBnClickedFindPath) ON_BN_CLICKED(IDOK, &SelectPathDlg::OnBnClickedOk) ON_BN_CLICKED(IDCANCEL, &SelectPathDlg::OnBnClickedCancel) END_MESSAGE_MAP() // SelectPathDlg 消息处理程序 void SelectPathDlg::OnBnClickedFindPath() { UpdateData(TRUE); CString newPath = openFilePathDlg(); if (newPath != "") { m_filePath = newPath; UpdateData(FALSE); } } void SelectPathDlg::OnBnClickedOk() { UpdateData(TRUE); saveSettings(); UpdateData(FALSE); HGTools::createDir(m_filePath); CDialogEx::OnOK(); } void SelectPathDlg::OnBnClickedCancel() { CDialogEx::OnCancel(); } BOOL SelectPathDlg::OnInitDialog() { CDialogEx::OnInitDialog(); loadSettings(); UpdateData(FALSE); return TRUE; // return TRUE unless you set the focus to a control // 异常: OCX 属性页应返回 FALSE } void SelectPathDlg::loadSettings() { //配置文件路径 CString hgmPath = HGTools::getRelativePath("kdata\Op.hgm"); CString xmlPath = HGTools::getXmlPathFromHgxPath(hgmPath); //解密 HGTools::decryptFile(hgmPath,xmlPath); //加载文件 TinyXml::TiXmlDocument doc(xmlPath); doc.LoadFile(); //加载辅助 TinyXml::XmlHandlePlus docHandler(&doc); docHandler.changeDirectoryTo("/Root/"); docHandler.setParamElementName("Param"); //读取数据 std::string str_filePath = docHandler.getParam("LastSavePath","C:\"); m_filePath.Format("%s",str_filePath.c_str()); //关闭文件 HGTools::deleteFile(xmlPath); } void SelectPathDlg::saveSettings() { //配置文件路径 CString hgmPath = HGTools::getRelativePath("kdata\Op.hgm"); CString xmlPath = HGTools::getXmlPathFromHgxPath(hgmPath); //解密 HGTools::decryptFile(hgmPath,xmlPath); //加载文件 TinyXml::TiXmlDocument doc(xmlPath); doc.LoadFile(); //加载辅助 TinyXml::XmlHandlePlus docHandler(&doc); docHandler.changeDirectoryTo("/Root/"); docHandler.setParamElementName("Param"); //保存数据 docHandler.setParam("LastSavePath",m_filePath.GetBuffer()); //保存 doc.SaveFile(xmlPath); HGTools::encryptFile(xmlPath,hgmPath); //关闭文件 HGTools::deleteFile(xmlPath); } #define BIF_NEWDIALOGSTYLE 0x40 int CALLBACK BrowseCallbackProc(HWND hwnd,UINT uMsg,LPARAM lParam,LPARAM lpData) { if(uMsg == BFFM_INITIALIZED) { SendMessage(hwnd, BFFM_SETSELECTION, TRUE, lpData); } return 0; } CString SelectPathDlg::openFilePathDlg() { CString pathName; char szDir[1000]; BROWSEINFO bi; ITEMIDLIST *pidl; bi.hwndOwner = this->m_hWnd; bi.pidlRoot = NULL; bi.pszDisplayName = szDir; bi.lpszTitle = _T("请选择目录"); bi.ulFlags = BIF_STATUSTEXT | BIF_USENEWUI | BIF_RETURNONLYFSDIRS; CFileFind finder; if(finder.FindFile(m_filePath)==FALSE) { bi.lParam =0; bi.lpfn = NULL; } else { bi.lParam = (long)(m_filePath.GetBuffer(m_filePath.GetLength()));//初始化路径,形如(_T("c:\Symbian")); bi.lpfn = BrowseCallbackProc; } finder.Close(); bi.iImage = 0; pidl = SHBrowseForFolder(&bi); if(pidl == NULL) return ""; if(!SHGetPathFromIDList(pidl, szDir)) return ""; pathName = szDir; return pathName; } //获得文件路径 CString SelectPathDlg::getFilePath() { return m_filePath; }