1

我希望用户能够编辑在我的 MFC 应用程序的文件菜单中显示的最近文件的数量。我使用了两个非常好的参考资料:

它涉及删除和重新创建CRecentFileList存储在CWinApp::m_pRecentFileList. 不幸的是,我发现在更换CRecentFileList. 请参阅下面的代码片段:

void CMyWinApp::SetMRUListSize( int size )
{
   // size guaranteed to be between 1 and 16
   delete m_pRecentFileList ;
   LoadStdProfileSettings( size ) ;
}

在我重新创建对象后,我可以做些什么来确保绘制到文件菜单中的内容与之同步m_pRecentFileList

4

2 回答 2

2

My CApp derives from CWinApp. In initInstance, you have this line:

LoadStdProfileSettings(10);

At the end of InitInstance, add this code:

m_pmf->m_pRecentFileList = m_pRecentFileList;

Here m_pmf is my MainFrame class and I created a member CMainFrame::m_pRecentFileList of type CRecentFileList which is in the MFC source file filelist.cpp. m_pRecentFileList on the right is protected and CMainFrame doesn't have access to it from outside InitInstance, but you can make a functional copy here.

At the end of CMainFrame::OnClose, force a registry update by:

           m_pRecentFileList->WriteList(); 

// Force registry update on exit. This doesn't work without forcing.

I don't even have to rebuild m_pRecentFileList, the MRU mechanism updates it correctly. Example: 5 MRU items, the first is moved to another directory and can no longer be found. Stepping through the code in the debugger shows that the bad entry is removed from the list. For some reason, the updated list isn't saved correctly unless I force it as explained above. I originally thought the problem might have something to do with privileges (64-bit Win7), but running the app as admin didn't help.

于 2012-11-03T07:49:22.043 回答
0

Microsoft 的一些文档建议您应该CWinApp::LoadStdProfileSettings从内部调用InitInstance. 这向我表明,这是在初始化期间而不是在运行时完成的事情。

您是否尝试过完全实现您提供的两个链接中的第二个?我的猜测是您需要添加第二部分而不是调用CWinApp::LoadStdProfileSettings

m_pRecentFileList = new CRecentFileList(0, strSection, strEntryFormat, nCount);
if(m_pRecentFileList)
{
    bReturn = TRUE;

    // Reload list of MRU files from registry
    m_pRecentFileList->ReadList();
}

[编辑]显然m_pRecentFileList指向一个CRecentFileList Class 。您是否尝试过调用CRecentFileList::UpdateMenu

还有另一个 CodeProject 示例也可能有所帮助

于 2009-05-27T23:08:52.200 回答