home *** CD-ROM | disk | FTP | other *** search
/ Mastering MFC Development / MMD.ISO / samples / c06 / modal / dialogs.cpp next >
Encoding:
C/C++ Source or Header  |  1997-02-20  |  5.3 KB  |  184 lines

  1. // Dialogs.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "modal.h"
  6.  
  7. #include "Dialogs.h"
  8. #include "ModalDoc.h"
  9.  
  10. #include "XtraFunc.h"
  11.  
  12. #ifdef _DEBUG
  13. #define new DEBUG_NEW
  14. #undef THIS_FILE
  15. static char THIS_FILE[] = __FILE__;
  16. #endif
  17.  
  18. /////////////////////////////////////////////////////////////////////////////
  19. // CColorListDlg dialog
  20.  
  21. CColorListDlg::CColorListDlg(COLORREF clr, CWnd* pParent /*=NULL*/)
  22.     : CDialog(CColorListDlg::IDD, pParent),
  23.     m_colorref(clr)
  24. {
  25.     //{{AFX_DATA_INIT(CColorListDlg)
  26.     m_phrase = _T("");
  27.     m_color = -1;
  28.     //}}AFX_DATA_INIT
  29. }
  30.  
  31. void CColorListDlg::DoDataExchange(CDataExchange* pDX)
  32. {
  33.     CDialog::DoDataExchange(pDX);
  34.     //{{AFX_DATA_MAP(CColorListDlg)
  35.     DDX_Control(pDX, IDC_COLOR_LIST, m_colorListBox);
  36.     DDX_Text(pDX, IDC_PHRASE, m_phrase);
  37.     DDX_LBIndex(pDX, IDC_COLOR_LIST, m_color);
  38.     //}}AFX_DATA_MAP
  39. }
  40.  
  41. BEGIN_MESSAGE_MAP(CColorListDlg, CDialog)
  42.     //{{AFX_MSG_MAP(CColorListDlg)
  43.     //}}AFX_MSG_MAP
  44. END_MESSAGE_MAP()
  45.  
  46. /////////////////////////////////////////////////////////////////////////////
  47. // CColorListDlg message handlers
  48. BOOL CColorListDlg::OnInitDialog() 
  49. {
  50.     CDialog::OnInitDialog();
  51.  
  52.     m_colorListBox.AddString("Black");
  53.     m_colorListBox.AddString("Red");
  54.     m_colorListBox.AddString("Green");
  55.     m_colorListBox.AddString("Blue");
  56.  
  57.     // In order to set the current selection in the list box, this
  58.     // dialog box needs to know the current color. The easiest way
  59.     // to communicate this information to the dialog box is to pass
  60.     // in it via the constructor, and store it in the member variable
  61.     // m_colorref. An alternative would be to obtain a pointer to the
  62.     // active document and then call the GetColor function from here.
  63.     // The method chosen is simpler.
  64.     m_colorListBox.SetCurSel(RgbToInt(m_colorref));
  65.     
  66.     // These are the steps needed to precisely set the height
  67.     // of the list box.
  68.     CRect rect;
  69.     int itemHeight;
  70.     m_colorListBox.GetClientRect(&rect);
  71.     m_colorListBox.MapWindowPoints(this, & rect);
  72.     itemHeight = m_colorListBox.GetItemHeight(0) + 1;
  73.     rect.bottom = rect.top + 4 * itemHeight;
  74.     m_colorListBox.MoveWindow(&rect);
  75.  
  76.     return TRUE;  // return TRUE unless you set the focus to a control
  77.                   // EXCEPTION: OCX Property Pages should return FALSE
  78. }
  79.  
  80. /////////////////////////////////////////////////////////////////////////////
  81. // CColorPhraseDlg dialog
  82. CColorPhraseDlg::CColorPhraseDlg(CWnd* pParent /*=NULL*/)
  83.     : CDialog(CColorPhraseDlg::IDD, pParent)
  84. {
  85.     //{{AFX_DATA_INIT(CColorPhraseDlg)
  86.     m_color = -1;
  87.     m_phrase = _T("");
  88.     //}}AFX_DATA_INIT
  89. }
  90.  
  91. void CColorPhraseDlg::DoDataExchange(CDataExchange* pDX)
  92. {
  93.     CDialog::DoDataExchange(pDX);
  94.     //{{AFX_DATA_MAP(CColorPhraseDlg)
  95.     DDX_Radio(pDX, IDC_BLACK, m_color);
  96.     DDX_Text(pDX, IDC_PHRASE, m_phrase);
  97.     //}}AFX_DATA_MAP
  98. }
  99.  
  100. BEGIN_MESSAGE_MAP(CColorPhraseDlg, CDialog)
  101.     //{{AFX_MSG_MAP(CColorPhraseDlg)
  102.         // NOTE: the ClassWizard will add message map macros here
  103.     //}}AFX_MSG_MAP
  104. END_MESSAGE_MAP()
  105.  
  106. /////////////////////////////////////////////////////////////////////////////
  107. // CColorPhraseDlg message handlers
  108.  
  109.  
  110. /////////////////////////////////////////////////////////////////////////////
  111. // CListWithItemDataDlg dialog
  112. CListWithItemDataDlg::CListWithItemDataDlg(CWnd* pParent /*=NULL*/)
  113.     : CDialog(CListWithItemDataDlg::IDD, pParent)
  114. {
  115.     //{{AFX_DATA_INIT(CListWithItemDataDlg)
  116.     m_phrase = _T("");
  117.     //}}AFX_DATA_INIT
  118. }
  119.  
  120. void CListWithItemDataDlg::DoDataExchange(CDataExchange* pDX)
  121. {
  122.     CDialog::DoDataExchange(pDX);
  123.     //{{AFX_DATA_MAP(CListWithItemDataDlg)
  124.     DDX_Text(pDX, IDC_PHRASE, m_phrase);
  125.     //}}AFX_DATA_MAP
  126.  
  127.     // If you want the list box's ItemData field available
  128.     // in the dialog class, you've got to do the transfer
  129.     // yourself
  130.     if (pDX->m_bSaveAndValidate)
  131.     {
  132.         CListBox * clb = (CListBox *)GetDlgItem(IDC_COLOR_LIST);
  133.         m_color = clb->GetItemData(clb->GetCurSel());
  134.     }
  135. }
  136.  
  137. BEGIN_MESSAGE_MAP(CListWithItemDataDlg, CDialog)
  138.     //{{AFX_MSG_MAP(CListWithItemDataDlg)
  139.     //}}AFX_MSG_MAP
  140. END_MESSAGE_MAP()
  141.  
  142. /////////////////////////////////////////////////////////////////////////////
  143. // CListWithItemDataDlg message handlers
  144.  
  145. BOOL CListWithItemDataDlg::OnInitDialog() 
  146. {
  147.     CDialog::OnInitDialog();
  148.     
  149.     // Since no Listbox member exists in the dialog class
  150.     // we'll access the list box via a pointer obtained
  151.     // from GetDlgItem.
  152.     CListBox * clb = (CListBox *)GetDlgItem(IDC_COLOR_LIST);
  153.     
  154.     // In this dialog box, the string is for appearance
  155.     // only. The real value of the user's selection will be
  156.     // the 32-bit value that is stored via SetItemData.
  157.     // If there were move than 4 entries in the list box,
  158.     // this section of code coudl be improved with some
  159.     // arrays and a for loop.
  160.     clb->AddString("Black");
  161.     clb->SetItemData(0, BLACK);
  162.     clb->AddString("Red");
  163.     clb->SetItemData(1, RED);
  164.     clb->AddString("Green");
  165.     clb->SetItemData(2, GREEN);
  166.     clb->AddString("Blue");
  167.     clb->SetItemData(3, BLUE);
  168.     
  169.     // Set the initial selection in the list box.
  170.     clb->SetCurSel(RgbToInt(m_color));
  171.  
  172.     // Size the list box.
  173.     CRect rect;
  174.     int itemHeight;
  175.     clb->GetClientRect(&rect);
  176.     clb->MapWindowPoints(this, & rect);
  177.     itemHeight = clb->GetItemHeight(0) + 1;
  178.     rect.bottom = rect.top + 4 * itemHeight;
  179.     clb->MoveWindow(&rect);
  180.     
  181.     return TRUE;  // return TRUE unless you set the focus to a control
  182.                   // EXCEPTION: OCX Property Pages should return FALSE
  183. }
  184.