0

我有一个 MFC C++ 应用程序,它有一个 CFileDialog。我调用它的 DoModal 函数来打开一个文件浏览窗口。我设置了 lp​​strInitialDir,告诉它第一次在哪里打开对话框

CString defaultDir = L"C:\\tmp\\";
CFileDialog d(TRUE);
d.m_ofn.lpstrInitialDir = defaultDir ;

if( d.DoModal ()==IDOK )
    {... app logic after the file was seslected...}

问题是我希望我的程序记住用户选择。下次用户运行我的应用程序时,我希望我的 DoModal 文件浏览对话框在用户上次使用时从中选择文件的位置打开。

我该怎么做?

我看到有 LastVisitedMRU 注册表项,但是我找不到任何示例如何正确使用 CFileDialog.DoModal

非常感谢!

4

1 回答 1

3

You won’t need to use “LastVisitedMRU” to accomplish this. Simply use the CWinApp::GetProfileString and CWinApp::WriteProfileString methods to read and write the path of the last accessed file. For example…</p>

CString defaultDir = AfxGetApp()->GetProfileString(_T(“&lt;registry key>"), _T("LastPath"));

CFileDialog d(TRUE);
d.m_ofn.lpstrInitialDir = defaultDir;
CString selectedPath = _T("");
BOOL rc = FALSE;

if (d.DoModal() == IDOK)
    {
    selectedPath = d.GetPathName();
    rc = AfxGetApp()->WriteProfileString(_T("<registry key>"), _T("LastPath"), selectedPath);
    }

Where, "registry key" is the value you used in the SetRegistry key call in your application's InitInstance method (if it’s not there, add it). And, "LastPath" is whatever you want for the registry sub-key.

NOTE: The sample code is from an MBCS project.

于 2015-07-29T17:41:03.050 回答