The EX23B Example -- An SDI Application

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.

  1. 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.

    click to view at full size.

  2. 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 } 

  3. 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 menu—You can have a Windows-based application without a menu—you 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 icon—An 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 handler—Many 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 bar—AppWizard 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 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 EX23B 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);



Programming Visual C++
Advanced 3ds max 5 Modeling & Animating
ISBN: 1572318570
EAN: 2147483647
Year: 1997
Pages: 331
Authors: Boris Kulagin

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net