home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / general / mdidocvw / mdi.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-27  |  5.3 KB  |  217 lines

  1. // MDI.cpp : Defines the class behaviors for the application.
  2. //
  3.  
  4. // This is a part of the Microsoft Foundation Classes C++ library.
  5. // Copyright (C) 1992-1998 Microsoft Corporation
  6. // All rights reserved.
  7. //
  8. // This source code is only intended as a supplement to the
  9. // Microsoft Foundation Classes Reference and related
  10. // electronic documentation provided with the library.
  11. // See these sources for detailed information regarding the
  12. // Microsoft Foundation Classes product.
  13.  
  14. #include "stdafx.h"
  15. #include "MDI.h"
  16.  
  17. #include "MainFrm.h"
  18. #include "HelloFrm.h"
  19. #include "HelloDoc.h"
  20. #include "HelloVw.h"
  21.  
  22. //Added for Bounce document
  23. #include "BncFrm.h"
  24. #include "BncDoc.h"
  25. #include "BncVw.h"
  26.  
  27. #ifdef _DEBUG
  28. #define new DEBUG_NEW
  29. #undef THIS_FILE
  30. static char THIS_FILE[] = __FILE__;
  31. #endif
  32.  
  33. /////////////////////////////////////////////////////////////////////////////
  34. // CMDIApp
  35.  
  36. BEGIN_MESSAGE_MAP(CMDIApp, CWinApp)
  37.     //{{AFX_MSG_MAP(CMDIApp)
  38.     ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
  39.     ON_COMMAND(ID_FILE_NEWHELLO, OnNewHello)
  40.     ON_COMMAND(ID_FILE_NEWBOUNCE, OnNewBounce)
  41.     //}}AFX_MSG_MAP
  42.     // Standard file based document commands
  43.     ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
  44.     ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
  45. END_MESSAGE_MAP()
  46.  
  47. /////////////////////////////////////////////////////////////////////////////
  48. // CMDIApp construction
  49.  
  50. CMDIApp::CMDIApp()
  51. {
  52. }
  53.  
  54. /////////////////////////////////////////////////////////////////////////////
  55. // The one and only CMDIApp object
  56.  
  57. CMDIApp theApp;
  58.  
  59. /////////////////////////////////////////////////////////////////////////////
  60. // CMDIApp initialization
  61.  
  62. BOOL CMDIApp::InitInstance()
  63. {
  64.     // Register the application's document templates.  Document templates
  65.     // serve as the connection between documents, frame windows and views.
  66.  
  67.     CMultiDocTemplate* pDocTemplate;
  68.     pDocTemplate = new CMultiDocTemplate(
  69.         IDR_HELLOTYPE,
  70.         RUNTIME_CLASS(CHelloDoc),
  71.         RUNTIME_CLASS(CHelloFrame), // custom MDI child frame
  72.         RUNTIME_CLASS(CHelloView));
  73.     AddDocTemplate(pDocTemplate);
  74.  
  75. // Add Bounce template to list
  76.  
  77.     CMultiDocTemplate* pBounceTemplate;
  78.     pBounceTemplate = new CMultiDocTemplate(
  79.         IDR_BOUNCETYPE,
  80.         RUNTIME_CLASS(CBounceDoc),
  81.         RUNTIME_CLASS(CBounceFrame), // custom MDI child frame
  82.         RUNTIME_CLASS(CBounceView));
  83.     AddDocTemplate(pBounceTemplate);
  84.  
  85.     // create main MDI Frame window
  86.     CMainFrame* pMainFrame = new CMainFrame;
  87.     if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
  88.         return FALSE;
  89.     m_pMainWnd = pMainFrame;
  90.  
  91.     // The main window has been initialized, so show and update it.
  92.     pMainFrame->ShowWindow(m_nCmdShow);
  93.     pMainFrame->UpdateWindow();
  94.  
  95.     return TRUE;
  96. }
  97.  
  98. /////////////////////////////////////////////////////////////////////////////
  99. // CAboutDlg dialog used for App About
  100.  
  101. class CAboutDlg : public CDialog
  102. {
  103. public:
  104.     CAboutDlg();
  105.  
  106. // Dialog Data
  107.     //{{AFX_DATA(CAboutDlg)
  108.     enum { IDD = IDD_ABOUTBOX };
  109.     //}}AFX_DATA
  110.  
  111.     // ClassWizard generated virtual function overrides
  112.     //{{AFX_VIRTUAL(CAboutDlg)
  113.     protected:
  114.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  115.     //}}AFX_VIRTUAL
  116.  
  117. // Implementation
  118. protected:
  119.     //{{AFX_MSG(CAboutDlg)
  120.         // No message handlers
  121.     //}}AFX_MSG
  122.     DECLARE_MESSAGE_MAP()
  123. };
  124.  
  125. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  126. {
  127.     //{{AFX_DATA_INIT(CAboutDlg)
  128.     //}}AFX_DATA_INIT
  129. }
  130.  
  131. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  132. {
  133.     CDialog::DoDataExchange(pDX);
  134.     //{{AFX_DATA_MAP(CAboutDlg)
  135.     //}}AFX_DATA_MAP
  136. }
  137.  
  138. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  139.     //{{AFX_MSG_MAP(CAboutDlg)
  140.         // No message handlers
  141.     //}}AFX_MSG_MAP
  142. END_MESSAGE_MAP()
  143.  
  144. // App command to run the dialog
  145. void CMDIApp::OnAppAbout()
  146. {
  147.     CAboutDlg aboutDlg;
  148.     aboutDlg.DoModal();
  149. }
  150.  
  151. /////////////////////////////////////////////////////////////////////////////
  152. // other globals
  153.  
  154. // Color array maps colors to top-level Color menu
  155.  
  156. COLORREF NEAR colorArray[] =
  157. {
  158.     RGB (0, 0, 0),
  159.     RGB (255, 0, 0),
  160.     RGB (0, 255, 0),
  161.     RGB (0, 0, 255),
  162.     RGB (255, 255, 255)
  163. };
  164. /////////////////////////////////////////////////////////////////////////////
  165. // CMDIApp commands
  166.  
  167. // The following two command handlers provides an
  168. // alternative way to open documents by hiding the fact
  169. // that the application has multiple templates. The
  170. // default method uses a dialog with a listing of
  171. // possible types to choose from.
  172.  
  173. void CMDIApp::OnNewHello()
  174. {
  175.  
  176. // Searches template list for a document type
  177. // containing the "Hello" string
  178.  
  179.     POSITION curTemplatePos = GetFirstDocTemplatePosition();
  180.  
  181.     while(curTemplatePos != NULL)
  182.     {
  183.         CDocTemplate* curTemplate =
  184.             GetNextDocTemplate(curTemplatePos);
  185.         CString str;
  186.         curTemplate->GetDocString(str, CDocTemplate::docName);
  187.         if(str == _T("Hello"))
  188.         {
  189.             curTemplate->OpenDocumentFile(NULL);
  190.             return;
  191.         }
  192.     }
  193.     AfxMessageBox(IDS_NOHELLOTEMPLATE);
  194. }
  195.  
  196. void CMDIApp::OnNewBounce()
  197. {
  198. // Searches template list for a document type
  199. // containing the "Bounce" string
  200.  
  201.     POSITION curTemplatePos = GetFirstDocTemplatePosition();
  202.  
  203.     while(curTemplatePos != NULL)
  204.     {
  205.         CDocTemplate* curTemplate =
  206.             GetNextDocTemplate(curTemplatePos);
  207.         CString str;
  208.         curTemplate->GetDocString(str, CDocTemplate::docName);
  209.         if(str == _T("Bounce"))
  210.         {
  211.             curTemplate->OpenDocumentFile(NULL);
  212.             return;
  213.         }
  214.     }
  215.     AfxMessageBox(IDS_NOBOUNCETEMPLATE);
  216. }
  217.