The Ex21b Example: An SDI Application
Chapter 2. The application has only one window—an object of a class derived from CFrameWnd. All drawing occurs inside the frame window, and all messages are handled there.
Run the MFC Application Wizard to produce the Ex21b project. Select the Single Document option on the Application Type page and deselect the Document/View Architecture Support option, as shown here:
Add code to paint in the view. Add the following boldface code to the CChildView::OnPaint function in the ChildView.cpp source code file:
void CChildView::OnPaint()
{
CPaintDC dc(this); // device context for painting
dc.TextOut(0, 0, "Hello, world!");
// Do not call CWnd::OnPaint() for painting messages
}
Compile and run the application. You now have a complete SDI application that has no dependencies on the document-view architecture.
The MFC Application Wizard automatically takes out dependencies on the document-view architecture and generates an application for you that has the following elements:
Main menu You can have a Windows-based application without a menu—you don't even need a resource script. But Ex21b has both. The application framework routes menu commands to message handlers in the frame class.
Icon An icon is useful if the program is to be activated from Windows Explorer. It's also useful when the application's main frame window is minimized. The icon is stored in the resource, along with the menu.
Window close message command handler Many applications need to do special processing when the main window is closed. If you're using documents, you can override the CDocument::SaveModified function. But here, to take control of the close process, the MFC Application Wizard creates message handlers to process close messages sent as a result of user actions and by Windows itself when it shuts down.
Toolbar and status bar The MFC Application Wizard automatically generates a default toolbar and status bar for you and sets up the routing even though there are no document-view classes.
Several interesting features in the SDI application have no document-view support, including:
CChildView class Contrary to its name, this class is actually a CWnd derivative that is declared in ChildView.h and implemented in ChildView.cpp . CChildView implements only a virtual OnPaint member function, which contains any code that you want to draw in the frame window (as illustrated in step 2 of the Ex21b sample).
CMainFrame class This class contains a data member, m_wndView, that is created and initialized in the CMainFrame::OnCreate member function.
CMainFrame::OnSetFocus function This function makes sure the focus is translated to the CChildView:
void CMainFrame::OnSetFocus(CWnd* pOldWnd)
{
// forward focus to the view window
m_wndView.SetFocus();
}
CMainFrame::OnCmdMsg function This function gives the view a chance to handle any command messages first:
BOOL CMainFrame::OnCmdMsg(UINT nID, int nCode, void* pExtra,
AFX_CMDHANDLERINFO* pHandlerInfo)
{
// let the view have first crack at the command
if (m_wndView.OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))
return TRUE;
// otherwise, do default handling
return CFrameWnd::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
}