home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / atl / mfcatl / mfcatldl.cpp < prev    next >
C/C++ Source or Header  |  1998-03-26  |  5KB  |  207 lines

  1. // MfcAtlDl.cpp : implementation file
  2. //
  3. // This is a part of the Active Template Library.
  4. // Copyright (C) 1996-1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Active Template Library Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Active Template Library product.
  12.  
  13. #include "premfcat.h"
  14. #include "MfcAtl.h"
  15. #include "MfcAtlDl.h"
  16.  
  17. #ifdef _DEBUG
  18. #define new DEBUG_NEW
  19. #undef THIS_FILE
  20. static char THIS_FILE[] = __FILE__;
  21. #endif
  22.  
  23. /////////////////////////////////////////////////////////////////////////////
  24. // CAboutDlg dialog used for App About
  25.  
  26. class CAboutDlg : public CDialog
  27. {
  28. public:
  29.     CAboutDlg();
  30.  
  31. // Dialog Data
  32.     //{{AFX_DATA(CAboutDlg)
  33.     enum { IDD = IDD_ABOUTBOX };
  34.     //}}AFX_DATA
  35.  
  36.     // ClassWizard generated virtual function overrides
  37.     //{{AFX_VIRTUAL(CAboutDlg)
  38.     protected:
  39.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  40.     //}}AFX_VIRTUAL
  41.  
  42. // Implementation
  43. protected:
  44.     //{{AFX_MSG(CAboutDlg)
  45.     //}}AFX_MSG
  46.     DECLARE_MESSAGE_MAP()
  47. };
  48.  
  49. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  50. {
  51.     //{{AFX_DATA_INIT(CAboutDlg)
  52.     //}}AFX_DATA_INIT
  53. }
  54.  
  55. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  56. {
  57.     CDialog::DoDataExchange(pDX);
  58.     //{{AFX_DATA_MAP(CAboutDlg)
  59.     //}}AFX_DATA_MAP
  60. }
  61.  
  62. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  63.     //{{AFX_MSG_MAP(CAboutDlg)
  64.         // No message handlers
  65.     //}}AFX_MSG_MAP
  66. END_MESSAGE_MAP()
  67.  
  68. /////////////////////////////////////////////////////////////////////////////
  69. // CMfcAtlDlg dialog
  70.  
  71. CMfcAtlDlg::CMfcAtlDlg(CWnd* pParent /*=NULL*/)
  72.     : CDialog(CMfcAtlDlg::IDD, pParent)
  73. {
  74.     //{{AFX_DATA_INIT(CMfcAtlDlg)
  75.         // NOTE: the ClassWizard will add member initialization here
  76.     //}}AFX_DATA_INIT
  77.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  78.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  79. }
  80.  
  81. void CMfcAtlDlg::DoDataExchange(CDataExchange* pDX)
  82. {
  83.     CDialog::DoDataExchange(pDX);
  84.     //{{AFX_DATA_MAP(CMfcAtlDlg)
  85.         // NOTE: the ClassWizard will add DDX and DDV calls here
  86.     //}}AFX_DATA_MAP
  87. }
  88.  
  89. BEGIN_MESSAGE_MAP(CMfcAtlDlg, CDialog)
  90.     //{{AFX_MSG_MAP(CMfcAtlDlg)
  91.     ON_WM_SYSCOMMAND()
  92.     ON_WM_PAINT()
  93.     ON_WM_QUERYDRAGICON()
  94.     ON_WM_CLOSE()
  95.     ON_BN_CLICKED(IDCLOSE, OnPressClose)
  96.     //}}AFX_MSG_MAP
  97. END_MESSAGE_MAP()
  98.  
  99. /////////////////////////////////////////////////////////////////////////////
  100. // CMfcAtlDlg message handlers
  101.  
  102. BOOL CMfcAtlDlg::OnInitDialog()
  103. {
  104.     CDialog::OnInitDialog();
  105.  
  106.     // Add "About..." menu item to system menu.
  107.  
  108.     // IDM_ABOUTBOX must be in the system command range.
  109.     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  110.     ASSERT(IDM_ABOUTBOX < 0xF000);
  111.  
  112.     CMenu* pSysMenu = GetSystemMenu(FALSE);
  113.     CString strAboutMenu;
  114.     strAboutMenu.LoadString(IDS_ABOUTBOX);
  115.     if (!strAboutMenu.IsEmpty())
  116.     {
  117.         pSysMenu->AppendMenu(MF_SEPARATOR);
  118.         pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  119.     }
  120.  
  121.     // Set the icon for this dialog.  The framework does this automatically
  122.     //  when the application's main window is not a dialog
  123.     SetIcon(m_hIcon, TRUE);         // Set big icon
  124.     SetIcon(m_hIcon, FALSE);        // Set small icon
  125.  
  126.     // TODO: Add extra initialization here
  127.  
  128.     return TRUE;  // return TRUE  unless you set the focus to a control
  129. }
  130.  
  131. void CMfcAtlDlg::OnSysCommand(UINT nID, LPARAM lParam)
  132. {
  133.     if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  134.     {
  135.         CAboutDlg dlgAbout;
  136.         dlgAbout.DoModal();
  137.     }
  138.     else
  139.     {
  140.         CDialog::OnSysCommand(nID, lParam);
  141.     }
  142. }
  143.  
  144. // If you add a minimize button to your dialog, you will need the code below
  145. //  to draw the icon.  For MFC applications using the document/view model,
  146. //  this is automatically done for you by the framework.
  147.  
  148. void CMfcAtlDlg::OnPaint()
  149. {
  150.     if (IsIconic())
  151.     {
  152.         CPaintDC dc(this); // device context for painting
  153.  
  154.         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  155.  
  156.         // Center icon in client rectangle
  157.         int cxIcon = GetSystemMetrics(SM_CXICON);
  158.         int cyIcon = GetSystemMetrics(SM_CYICON);
  159.         CRect rect;
  160.         GetClientRect(&rect);
  161.         int x = (rect.Width() - cxIcon + 1) / 2;
  162.         int y = (rect.Height() - cyIcon + 1) / 2;
  163.  
  164.         // Draw the icon
  165.         dc.DrawIcon(x, y, m_hIcon);
  166.     }
  167.     else
  168.     {
  169.         CDialog::OnPaint();
  170.     }
  171. }
  172.  
  173. // The system calls this to obtain the cursor to display while the user drags
  174. //  the minimized window.
  175. HCURSOR CMfcAtlDlg::OnQueryDragIcon()
  176. {
  177.     return (HCURSOR) m_hIcon;
  178. }
  179.  
  180. void CMfcAtlDlg::OnClose()
  181. {
  182.     CWinApp* pApp = AfxGetApp();
  183.     ASSERT(pApp->m_pMainWnd == this);
  184.     ASSERT(pApp->m_pMainWnd->IsWindowVisible());
  185.  
  186.     // don't exit if there are outstanding component objects
  187.     if (!AfxOleCanExitApp())
  188.     {
  189.         // take user out of control of the app
  190.         AfxOleSetUserCtrl(FALSE);
  191.         // don't destroy the window and close down, just hide
  192.         ShowWindow(SW_HIDE);
  193.         return;
  194.     }
  195.     DestroyWindow();
  196. }
  197.  
  198. void CMfcAtlDlg::PostNcDestroy()
  199. {
  200.     delete this;
  201. }
  202.  
  203. void CMfcAtlDlg::OnPressClose()
  204. {
  205.     PostMessage(WM_CLOSE);
  206. }
  207.