EQuestionAnswers.com Computer/Electronics Questions and Answers
C, C++, VC++, COM/DCOM, DLL and more
#Login #Sign up  Facebook Twitter TGoogle+
 

#Previous question #Index of VC++/WIN32/MFC Questions #Next question

We have discussed the basic workflow of a minimal doc-view application. We understood the functionalities and responsibilities of each class in doc-view. Developer should implement the responsible OnXXX() user events for each classes and MFC framework internally calls these event.

Now lets understand how MFC message framework routes messages to these classes and how event functions are called from framework.

CFrameWnd class creates the main top level window in the application. Thus all the messages will be submitted to the window procedure of this class. CFrameWnd is derived from the CCmdTarget class and all the messages will be routed to the OnCmdMsg virtual function. CFrameWnd::OnCmdMsg function processes the exection in this way-

  • Process the message in View class
  • Process the message in Frame
  • Process the message in Application class

Message Handler

// CFrameWnd command/message routing

BOOL CFrameWnd::OnCmdMsg(UINT nID, int nCode, void* pExtra,
	AFX_CMDHANDLERINFO* pHandlerInfo)
{
	CPushRoutingFrame push(this);

	// pump through current view FIRST
	CView* pView = GetActiveView();
	if (pView != NULL && pView->OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))
		return TRUE;

	// then pump through frame
	if (CWnd::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))
		return TRUE;

	// last but not least, pump through app
	CWinApp* pApp = AfxGetApp();
	if (pApp != NULL && pApp->OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))
		return TRUE;

	return FALSE;
}

New Document Event @ startup

New document event is called at the launch of application or when user selects new document menu/toolbar icon.

At the starting of the application this event is called directly from main entry point. This is bacause CWinnApp is still in InitInstance() call and main window frame is still not yet created. Event OnFileNew() is called from the shell command line processing function.

void CWinApp::OnFileNew()
{
  if (m_pDocManager != NULL)
    m_pDocManager->OnFileNew();
}

Open Document callstack-

CMySDIDoc::OnNewDocument()
CSingleDocTemplate::OpenDocumentFile()
CDocManager::OnFileNew()
CWinApp::OnFileNew()
_AfxDispatchCmdMsg()
CCmdTarget::OnCmdMsg()
CWinApp::ProcessShellCommand()
CMySDIApp::InitInstance()
AfxWinMain()
WinMain()
WinMainCRTStartup()

New Document Event @ user command

User can trigger this event by clicking on new menu item or clicking on new button in the toolbar. Then this is routed through the message handling of the main frame window.

void CWinApp::OnFileNew()
{
  if (m_pDocManager != NULL)
    m_pDocManager->OnFileNew();
}

Open Document callstack-

CMySDIDoc::OnNewDocument()
CSingleDocTemplate::OpenDocumentFile()
CDocManager::OnFileNew()
CWinApp::OnFileNew()
_AfxDispatchCmdMsg()
CCmdTarget::OnCmdMsg()
CFrameWnd::OnCmdMsg()
CWnd::OnCommand()
CFrameWnd::OnCommand()
CWnd::OnWndMsg()
CWnd::WindowProc()
AfxCallWndProc()
AfxWndProc()
AfxWndProcBase()

Open Document Event

An open document event is called when user clicks on open menu or open icon in toolbar. Open document event calls CDocument::Serialize() which loads file content to document.
void CWinApp::OnFileOpen()
{
  ASSERT(m_pDocManager != NULL);
  m_pDocManager->OnFileOpen();
}

Open Document callstack-

CMySDIDoc::Serialize()
CDocument::OnOpenDocument()
CSingleDocTemplate::OpenDocumentFile()
CDocManager::OpenDocumentFile()
CWinApp::OpenDocumentFile()
CDocManager::OnFileOpen()
CWinApp::OnFileOpen()
_AfxDispatchCmdMsg()
CCmdTarget::OnCmdMsg()
CFrameWnd::OnCmdMsg()
CWnd::OnCommand()
CFrameWnd::OnCommand()
CWnd::OnWndMsg()
CWnd::WindowProc()
AfxCallWndProc()
AfxWndProc()
AfxWndProcBase()

Save Document Event

Save document event is called when user clicks on save menu or save icon in toolbar. Save document event calls CDocument::Serialize() which finally saves file content to document.

Save Document callstack-

CMySDIDoc::Serialize()
CDocument::OnSaveDocument()
CDocument::DoSave()
CDocument::DoFileSave()
CDocument::OnFileSave()
_AfxDispatchCmdMsg()
CCmdTarget::OnCmdMsg()
CDocument::OnCmdMsg()
CView::OnCmdMsg()
CFrameWnd::OnCmdMsg()
CWnd::OnCommand()
CFrameWnd::OnCommand()
CWnd::OnWndMsg()
CWnd::WindowProc()
AfxCallWndProc()
AfxWndProc()
AfxWndProcBase()

You have viewed 1 page out of 109. Your VC++ learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

#Programming Windows, Fifth Edition (Developer Reference)
#Programming Applications for Microsoft Windows (Microsoft Programming Series)
#Programming Windows with MFC, Second Edition
#Visual C++ Programming
#Programming Microsoft Visual C++, Fifth Edition

* #1 webmaster Sun 27 Apr/2014 16:28:09(GMT)  Like 0 Unlike 0

Dear Users,

We are pleased to inform you that a forum/blog has been incorporated with www.mybestnotes.co.in. You are welcomed to add your comments, requests, codes, solutions and feedback to it. Please login(if already a member) or signup(for free) to avail to this facility.

Regards,
Webmaster

Your message goes here:

Name:Guest
Email:anonymous@unknown.com
My Post:*
Secutiry Code: ******  *
Preview this compose before posting this in discussion forum.
 
Note:
  1. Use [Sxx] code to show smiles. Example [S02]
  2. Use [URL ], example [URL http://www.google.com/]
  3. To display code or un formatted text use [CODE] [/CODE]. Example: [CODE] printf("Hello world"); [/CODE]
 [S01]   [S02]   [S03]   [S04]   [S05]   [S06]   [S07]   [S08] 
 [S09]   [S10]   [S11]   [S12]   [S13]   [S14]   [S15]   [S16] 
 [S17]   [S18]   [S19]   [S20]   [S21]   [S22]   [S23]   [S24] 

Note: Only members are allowed, * fields are mandatory.