Additional Combo MRU for Doc/View architecture






4.29/5 (6 votes)
May 2, 2003

44691

612
Doc/View MRU reflected in a ComboBox on the document form view
Introduction
One day I had to create an SDI application that requires an MRU feature implemented in a combo box on the document form view. I'm not an advanced VC programmer so I don't pretend that this is the best solution. This article is for beginners that search for such an example.
What is to be done ?
Create an usual SDI application using VC wizard. For my project I specified an MRU size of 5.
Override the default OnCmdMsg
message dispatcher of your application object (CWinApp
derived) using the Class Wizard. In my example the application object is CTestFormApp
.
BOOL CTestFormApp::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo) { BOOL temp = CWinApp::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo); if (nID >= ID_FILE_MRU_FILE1 && nID <= ID_FILE_MRU_FILE1 + 4) if (nCode == CN_COMMAND) UpdateComboMRU(); return temp; }
The implementation of the UpdateComboMRU
looks like this:
#include "afxpriv.h" // needed because there is a forward class //declaration of CRecentFileList CTestFormApp::UpdateComboMRU() { CComboBox & combo=((CTestFormView*)( (CFrameWnd*)m_pMainWnd)->GetActiveView())->m_ComboMRU; CString x; combo.ResetContent(); for (int i = 0; i < m_pRecentFileList->m_nSize; i++) { if (!m_pRecentFileList->GetDisplayName(x, i, NULL, NULL)) break; combo.AddString(x); // update the combo from MRU object } combo.SetCurSel(0); // select first MRU file }
CTestFormView
is my document view class and m_ComboMRU
is a CComboBox
object displayed in the view.
In order to work things out the following calls are needed:
((CTestFormApp*)AfxGetApp())->UpdateComboMRU();
from:
- the form view
OnInitialUpdate
- the document
OnOpenDocument
- the document
DoSave
just like this:
void CTestFormView::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
((CTestFormApp*)AfxGetApp())->UpdateComboMRU();
GetParentFrame()->RecalcLayout();
ResizeParentToFit();
}
BOOL CTestFormDoc::OnOpenDocument(LPCTSTR lpszPathName) { if (!CDocument::OnOpenDocument(lpszPathName)) return FALSE; ((CTestFormApp*)AfxGetApp())->UpdateComboMRU(); return TRUE; }
BOOL CTestFormDoc::DoSave(LPCTSTR lpszPathName, BOOL bReplace)
{
BOOL temp = CDocument::DoSave(lpszPathName, bReplace);
((CTestFormApp*)AfxGetApp())->UpdateComboMRU();
return temp;
}
You need to override DoSave
. There's an interesting article about this on this site:
- CDocument::DoSave revealed by Nishant S