Click here to Skip to main content
15,891,597 members
Articles / Desktop Programming / MFC
Article

Additional Combo MRU for Doc/View architecture

Rate me:
Please Sign up or sign in to vote.
4.29/5 (6 votes)
1 May 2003 44.6K   612   12   1
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:

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Germany Germany
none

Comments and Discussions

 
GeneralGood work Pin
Portatofe2-Oct-08 5:57
Portatofe2-Oct-08 5:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.