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

  1. // modeldlg.cpp : implementation file
  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 "modeless.h"
  16. #include "adderdlg.h"
  17. #include "modeldlg.h"
  18.  
  19. #ifdef _debug
  20. #undef this_file
  21. static char based_code THIS_FILE[] = __FILE__;
  22. #endif
  23.  
  24. /////////////////////////////////////////////////////////////////////////////
  25. // CAboutDlg dialog used for App About
  26.  
  27. class CAboutDlg : public CDialog
  28. {
  29. public:
  30.     CAboutDlg();
  31.  
  32. // Dialog Data
  33.     //{{AFX_DATA(CAboutDlg)
  34.     enum { IDD = IDD_ABOUTBOX };
  35.     //}}AFX_DATA
  36.  
  37. // Implementation
  38. protected:
  39.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  40.     //{{AFX_MSG(CAboutDlg)
  41.     //}}AFX_MSG
  42.     DECLARE_MESSAGE_MAP()
  43. };
  44.  
  45. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  46. {
  47.     //{{AFX_DATA_INIT(CAboutDlg)
  48.     //}}AFX_DATA_INIT
  49. }
  50.  
  51. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  52. {
  53.     CDialog::DoDataExchange(pDX);
  54.     //{{AFX_DATA_MAP(CAboutDlg)
  55.     //}}AFX_DATA_MAP
  56. }
  57.  
  58. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  59.     //{{AFX_MSG_MAP(CAboutDlg)
  60.         // No message handlers
  61.     //}}AFX_MSG_MAP
  62. END_MESSAGE_MAP()
  63.  
  64. /////////////////////////////////////////////////////////////////////////////
  65. // CMainDlg dialog
  66.  
  67. CMainDlg::CMainDlg(CWnd* pParent /*=NULL*/)
  68.     : CDialog(CMainDlg::IDD, pParent)
  69. {
  70.     //{{AFX_DATA_INIT(CMainDlg)
  71.         // NOTE: the ClassWizard will add member initialization here
  72.     //}}AFX_DATA_INIT
  73.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  74.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  75.     m_pModeless = NULL;
  76. }
  77.  
  78. void CMainDlg::DoDataExchange(CDataExchange* pDX)
  79. {
  80.     CDialog::DoDataExchange(pDX);
  81.     //{{AFX_DATA_MAP(CMainDlg)
  82.         // NOTE: the ClassWizard will add DDX and DDV calls here
  83.     //}}AFX_DATA_MAP
  84. }
  85.  
  86. BEGIN_MESSAGE_MAP(CMainDlg, CDialog)
  87.     //{{AFX_MSG_MAP(CMainDlg)
  88.     ON_WM_SYSCOMMAND()
  89.     ON_WM_PAINT()
  90.     ON_WM_QUERYDRAGICON()
  91.     //}}AFX_MSG_MAP
  92. END_MESSAGE_MAP()
  93.  
  94. /////////////////////////////////////////////////////////////////////////////
  95. // CMainDlg message handlers
  96.  
  97. BOOL CMainDlg::OnInitDialog()
  98. {
  99.     CDialog::OnInitDialog(  );
  100.  
  101.     // Add "About..." menu item to system menu.
  102.  
  103.     // IDM_ABOUTBOX must be in the system command range.
  104.     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  105.     ASSERT(IDM_ABOUTBOX < 0xF000);
  106.  
  107.     CMenu* pSysMenu = GetSystemMenu(FALSE);
  108.     CString strAboutMenu;
  109.     strAboutMenu.LoadString(IDS_ABOUTBOX);
  110.     if (!strAboutMenu.IsEmpty())
  111.     {
  112.         pSysMenu->AppendMenu(MF_SEPARATOR);
  113.         pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  114.     }
  115.  
  116.     // TODO: Add extra initialization here
  117.  
  118.     return TRUE;  // return TRUE  unless you set the focus to a control
  119. }
  120.  
  121. void CMainDlg::OnSysCommand(UINT nID, LPARAM lParam)
  122. {
  123.     if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  124.     {
  125.         CAboutDlg dlgAbout;
  126.         dlgAbout.DoModal();
  127.     }
  128.     else
  129.     {
  130.         CDialog::OnSysCommand(nID, lParam);
  131.     }
  132. }
  133.  
  134. // If you add a minimize button to your dialog, you will need the code below
  135. //  to draw the icon.  For MFC applications using the document/view model,
  136. //  this is automatically done for you by the framework.
  137.  
  138. void CMainDlg::OnPaint()
  139. {
  140.     if (IsIconic())
  141.     {
  142.         CPaintDC dc(this); // device context for painting
  143.  
  144.         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  145.  
  146.         // Center icon in client rectangle
  147.         int cxIcon = GetSystemMetrics(SM_CXICON);
  148.         int cyIcon = GetSystemMetrics(SM_CYICON);
  149.         CRect rect;
  150.         GetClientRect(&rect);
  151.         int x = (rect.Width() - cxIcon + 1) / 2;
  152.         int y = (rect.Height() - cyIcon + 1) / 2;
  153.  
  154.         // Draw the icon
  155.         dc.DrawIcon(x, y, m_hIcon);
  156.     }
  157.     else
  158.     {
  159.         CDialog::OnPaint();
  160.     }
  161. }
  162.  
  163. // The system calls this to obtain the cursor to display while the user drags
  164. //  the minimized window.
  165. HCURSOR CMainDlg::OnQueryDragIcon()
  166. {
  167.     return (HCURSOR) m_hIcon;
  168. }
  169.  
  170. void CMainDlg::OnOK()
  171. {
  172.     // if our modeless child isn't already up, create it and display it
  173.     // otherwise, just set focus to it
  174.  
  175.     if (m_pModeless == NULL)
  176.     {
  177.         m_pModeless = new CAdderDialog(this);
  178.         if (m_pModeless->Create() == TRUE)
  179.             GetDlgItem(IDOK)->EnableWindow(FALSE);
  180.     }
  181.     else
  182.         m_pModeless->SetActiveWindow();
  183. }
  184.  
  185.  
  186. void CMainDlg::BoxDone()
  187. {
  188.     // this function is called by the modeless dialog as it terminates
  189.     // just reset our pushbutton to be enabled again.
  190.     // I _don't_ delete the MFC CDialog object because the dialog's own
  191.     // code will do that.
  192.  
  193.     m_pModeless = NULL;
  194.     // don't delete m_pModeless; !
  195.     GetDlgItem(IDOK)->EnableWindow();
  196. }
  197.