This SDI "Hello, world!" example builds on the code you saw way back in 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 AppWizard to produce \vcpp32\ex23b\ex23b.
Select the Single Document option in the AppWizard Step 1 dialog and uncheck the Document/View Architecture Support? option, as shown here.
- Add code to paint in the dialog.
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.
You now have a complete SDI application that has no dependencies on the document-view architecture.
AppWizard automatically takes out dependencies on the document-view architecture and generates an application for you with the following elements:
- A main menuYou can have a Windows-based application without a menuyou don't even need a resource script. But EX23B has both. The application framework routes menu commands to message handlers in the frame class.
- An iconAn icon is useful if the program is to be activated from Microsoft 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 handlerMany an application needs to do special processing when its main window is closed. If you were using documents, you could override the CDocument::SaveModified function. Here, to take control of the close process, AppWizard 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 barAppWizard automatically generates a default toolbar and status bar for you and sets up the routing even though there are no document-view classes.
There are several interesting features in the SDI application that have no document-view support, including:
- CChildView classContrary 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 EX23B sample).
- CMainFrame classThis class contains a data member, m_wndView, that is created and initialized in the CMainFrame::OnCreate member function.
- CMainFrame::OnSetFocus functionThis 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 functionThis 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);