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

  1. // dlgtempDlg.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 "dlgtemp.h"
  16. #include "setupdlg.h"
  17.  
  18. #ifdef _DEBUG
  19. #define new DEBUG_NEW
  20. #undef THIS_FILE
  21. static char 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.     // ClassWizard generated virtual function overrides
  38.     //{{AFX_VIRTUAL(CAboutDlg)
  39.     protected:
  40.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  41.     //}}AFX_VIRTUAL
  42.  
  43. // Implementation
  44. protected:
  45.     //{{AFX_MSG(CAboutDlg)
  46.     //}}AFX_MSG
  47.     DECLARE_MESSAGE_MAP()
  48. };
  49.  
  50. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  51. {
  52.     //{{AFX_DATA_INIT(CAboutDlg)
  53.     //}}AFX_DATA_INIT
  54. }
  55.  
  56. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  57. {
  58.     CDialog::DoDataExchange(pDX);
  59.     //{{AFX_DATA_MAP(CAboutDlg)
  60.     //}}AFX_DATA_MAP
  61. }
  62.  
  63. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  64.     //{{AFX_MSG_MAP(CAboutDlg)
  65.         // No message handlers
  66.     //}}AFX_MSG_MAP
  67. END_MESSAGE_MAP()
  68.  
  69. /////////////////////////////////////////////////////////////////////////////
  70. // CDlgtempDlg dialog
  71.  
  72. CDlgtempDlg::CDlgtempDlg(CWnd* pParent /*=NULL*/)
  73.     : CDialog(CDlgtempDlg::IDD, pParent)
  74. {
  75.     m_nActiveItem = -1;
  76.  
  77.     //{{AFX_DATA_INIT(CDlgtempDlg)
  78.     m_strCaption = _T("");
  79.     m_x = 0;
  80.     m_y = 0;
  81.     m_nWidth = 0;
  82.     m_nHeight = 0;
  83.     m_nItem = -1;
  84.     m_strSelectedItem = _T("");
  85.     //}}AFX_DATA_INIT
  86.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  87.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  88. }
  89.  
  90. void CDlgtempDlg::DoDataExchange(CDataExchange* pDX)
  91. {
  92.     CDialog::DoDataExchange(pDX);
  93.     //{{AFX_DATA_MAP(CDlgtempDlg)
  94.     DDX_Text(pDX, IDC_CAPTION, m_strCaption);
  95.     DDX_Text(pDX, IDC_XPOS, m_x);
  96.     DDV_MinMaxUInt(pDX, m_x, 0, 300);
  97.     DDX_Text(pDX, IDC_YPOS, m_y);
  98.     DDV_MinMaxUInt(pDX, m_y, 0, 200);
  99.     DDX_Text(pDX, IDC_WIDTH, m_nWidth);
  100.     DDX_Text(pDX, IDC_HEIGHT, m_nHeight);
  101.     DDX_Radio(pDX, IDC_BUTTON, m_nItem);
  102.     DDX_Text(pDX, IDC_SELECTEDITEM, m_strSelectedItem);
  103.     //}}AFX_DATA_MAP
  104. }
  105.  
  106. BEGIN_MESSAGE_MAP(CDlgtempDlg, CDialog)
  107.     //{{AFX_MSG_MAP(CDlgtempDlg)
  108.     ON_WM_SYSCOMMAND()
  109.     ON_WM_PAINT()
  110.     ON_BN_CLICKED(IDC_DEMOIT, OnDemoIt)
  111.     ON_BN_CLICKED(IDC_BUTTON, OnButtonInformation)
  112.     ON_BN_CLICKED(IDC_STATICTEXT, OnStaticTextInformation)
  113.     ON_BN_CLICKED(IDC_EDITCONTROL, OnEditControlInformation)
  114.     //}}AFX_MSG_MAP
  115. END_MESSAGE_MAP()
  116.  
  117. /////////////////////////////////////////////////////////////////////////////
  118. // CDlgtempDlg message handlers
  119.  
  120. BOOL CDlgtempDlg::OnInitDialog()
  121. {
  122.     CDialog::OnInitDialog();
  123.  
  124.     // Add "About..." menu item to system menu.
  125.  
  126.     // IDM_ABOUTBOX must be in the system command range.
  127.     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  128.     ASSERT(IDM_ABOUTBOX < 0xF000);
  129.  
  130.     CMenu* pSysMenu = GetSystemMenu(FALSE);
  131.     CString strAboutMenu;
  132.     strAboutMenu.LoadString(IDS_ABOUTBOX);
  133.     if (!strAboutMenu.IsEmpty())
  134.     {
  135.         pSysMenu->AppendMenu(MF_SEPARATOR);
  136.         pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  137.     }
  138.  
  139.     // Set the icon for this dialog.  The framework does this automatically
  140.     //  when the application's main window is not a dialog
  141.     SetIcon(m_hIcon, TRUE);         // Set big icon
  142.     SetIcon(m_hIcon, FALSE);        // Set small icon
  143.  
  144.     m_nItem = 0;        // select the BUTTON option
  145.     OnButtonInformation();  // fill the edit fields with corresponding values
  146.  
  147.     return TRUE;  // return TRUE  unless you set the focus to a control
  148. }
  149.  
  150. void CDlgtempDlg::OnSysCommand(UINT nID, LPARAM lParam)
  151. {
  152.     if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  153.     {
  154.         CAboutDlg dlgAbout;
  155.         dlgAbout.DoModal();
  156.     }
  157.     else
  158.     {
  159.         CDialog::OnSysCommand(nID, lParam);
  160.     }
  161. }
  162.  
  163. // If you add a minimize button to your dialog, you will need the code below
  164. //  to draw the icon.  For MFC applications using the document/view model,
  165. //  this is automatically done for you by the framework.
  166.  
  167. void CDlgtempDlg::OnPaint()
  168. {
  169.     if (IsIconic())
  170.     {
  171.         CPaintDC dc(this); // device context for painting
  172.  
  173.         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  174.  
  175.         // Center icon in client rectangle
  176.         int cxIcon = GetSystemMetrics(SM_CXICON);
  177.         int cyIcon = GetSystemMetrics(SM_CYICON);
  178.         CRect rect;
  179.         GetClientRect(&rect);
  180.         int x = (rect.Width() - cxIcon + 1) / 2;
  181.         int y = (rect.Height() - cyIcon + 1) / 2;
  182.  
  183.         // Draw the icon
  184.         dc.DrawIcon(x, y, m_hIcon);
  185.     }
  186.     else
  187.     {
  188.         CDialog::OnPaint();
  189.     }
  190. }
  191.  
  192. void CDlgtempDlg::OnDemoIt()
  193. {
  194.     InfoToDialogHelper(m_nActiveItem, NULL);
  195.     m_dlgTemplate.DemoIt();
  196. }
  197.  
  198. void CDlgtempDlg::OnButtonInformation()
  199. {
  200.     // take the information about the button and send it to the edit
  201.     // controls in the dialog.
  202.     InfoToDialogHelper(IBUTTON, _T("BUTTON"));
  203. }
  204.  
  205. void CDlgtempDlg::OnStaticTextInformation()
  206. {
  207.     InfoToDialogHelper(ISTATICTEXT, _T("STATIC TEXT"));
  208. }
  209.  
  210. void CDlgtempDlg::OnEditControlInformation()
  211. {
  212.     InfoToDialogHelper(IEDITCONTROL, _T("EDIT CONTROL"));
  213. }
  214.  
  215. void CDlgtempDlg::InfoToDialogHelper(int nItem, LPCTSTR lpszSelection)
  216. {
  217.     // first bring data from edit field and store in proper item
  218.     if (m_nActiveItem != -1)
  219.     {
  220.         UpdateData(TRUE/*bSaveAndValidate*/);
  221.         m_dlgTemplate.m_rgDlgItem[m_nActiveItem].m_dlgItemTemplate.x = m_x;
  222.         m_dlgTemplate.m_rgDlgItem[m_nActiveItem].m_dlgItemTemplate.y = m_y;
  223.         m_dlgTemplate.m_rgDlgItem[m_nActiveItem].m_dlgItemTemplate.cx = m_nWidth;
  224.         m_dlgTemplate.m_rgDlgItem[m_nActiveItem].m_dlgItemTemplate.cy = m_nHeight;
  225.         m_dlgTemplate.m_rgDlgItem[m_nActiveItem].m_strCaption = m_strCaption;
  226.     }
  227.  
  228.     // now bring data from item and send to edit fields
  229.     if (m_nActiveItem != nItem)
  230.     {
  231.         m_x = m_dlgTemplate.m_rgDlgItem[nItem].m_dlgItemTemplate.x;
  232.         m_y = m_dlgTemplate.m_rgDlgItem[nItem].m_dlgItemTemplate.y;
  233.         m_nWidth = m_dlgTemplate.m_rgDlgItem[nItem].m_dlgItemTemplate.cx;
  234.         m_nHeight = m_dlgTemplate.m_rgDlgItem[nItem].m_dlgItemTemplate.cy;
  235.         m_strCaption = m_dlgTemplate.m_rgDlgItem[nItem].m_strCaption;
  236.         ASSERT(lpszSelection != NULL);
  237.         m_strSelectedItem = lpszSelection;
  238.         m_nItem = nItem;
  239.         m_nActiveItem = nItem;
  240.         UpdateData(FALSE/*bSaveAndValidate*/);
  241.     }
  242.  
  243.     CWnd*   pwnd = GetDlgItem(IDC_CAPTION);
  244.     pwnd->EnableWindow(nItem != IEDITCONTROL);
  245. }
  246.