home *** CD-ROM | disk | FTP | other *** search
/ Mastering MFC Development / MMD.ISO / samples / c07 / form / formview.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-20  |  4.7 KB  |  185 lines

  1. // FormView.cpp : implementation of the CMyFormView class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "Form.h"
  6.  
  7. #include "FormDoc.h"
  8. #include "FormView.h"
  9.  
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15.  
  16. /////////////////////////////////////////////////////////////////////////////
  17. // CMyFormView
  18.  
  19. IMPLEMENT_DYNCREATE(CMyFormView, CFormView)
  20.  
  21. BEGIN_MESSAGE_MAP(CMyFormView, CFormView)
  22.     //{{AFX_MSG_MAP(CMyFormView)
  23.     ON_EN_CHANGE(IDC_WELCOME, OnChangeWelcome)
  24.     ON_WM_CTLCOLOR()
  25.     //}}AFX_MSG_MAP
  26.     // Standard printing commands
  27.     ON_CONTROL_RANGE(BN_CLICKED, IDC_RED, IDC_BLUE, OnColor)
  28.     ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
  29.     ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
  30.     ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
  31. END_MESSAGE_MAP()
  32.  
  33. /////////////////////////////////////////////////////////////////////////////
  34. // CMyFormView construction/destruction
  35.  
  36. CMyFormView::CMyFormView()
  37.     : CFormView(CMyFormView::IDD)
  38. {
  39.     //{{AFX_DATA_INIT(CMyFormView)
  40.     m_dlgPhrase = _T("");
  41.     m_blue = FALSE;
  42.     m_green = FALSE;
  43.     m_red = FALSE;
  44.     //}}AFX_DATA_INIT
  45.     // TODO: add construction code here
  46. }
  47.  
  48. CMyFormView::~CMyFormView()
  49. {
  50. }
  51.  
  52. void CMyFormView::DoDataExchange(CDataExchange* pDX)
  53. {
  54.     CFormView::DoDataExchange(pDX);
  55.     //{{AFX_DATA_MAP(CMyFormView)
  56.     DDX_Text(pDX, IDC_WELCOME, m_dlgPhrase);
  57.     DDX_Check(pDX, IDC_BLUE, m_blue);
  58.     DDX_Check(pDX, IDC_GREEN, m_green);
  59.     DDX_Check(pDX, IDC_RED, m_red);
  60.     //}}AFX_DATA_MAP
  61. }
  62.  
  63. BOOL CMyFormView::PreCreateWindow(CREATESTRUCT& cs)
  64. {
  65.     return CFormView::PreCreateWindow(cs);
  66. }
  67.  
  68. /////////////////////////////////////////////////////////////////////////////
  69. // CMyFormView printing
  70.  
  71. BOOL CMyFormView::OnPreparePrinting(CPrintInfo* pInfo)
  72. {
  73.     return DoPreparePrinting(pInfo);
  74. }
  75.  
  76. void CMyFormView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  77. {
  78. }
  79.  
  80. void CMyFormView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  81. {
  82. }
  83.  
  84. void CMyFormView::OnPrint(CDC* pDC, CPrintInfo*)
  85. {
  86. }
  87.  
  88. /////////////////////////////////////////////////////////////////////////////
  89. // CMyFormView diagnostics
  90.  
  91. #ifdef _DEBUG
  92. void CMyFormView::AssertValid() const
  93. {
  94.     CFormView::AssertValid();
  95. }
  96.  
  97. void CMyFormView::Dump(CDumpContext& dc) const
  98. {
  99.     CFormView::Dump(dc);
  100. }
  101.  
  102. CFormDoc* CMyFormView::GetDocument() // non-debug version is inline
  103. {
  104.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CFormDoc)));
  105.     return (CFormDoc*)m_pDocument;
  106. }
  107. #endif //_DEBUG
  108.  
  109. /////////////////////////////////////////////////////////////////////////////
  110. // CMyFormView message handlers
  111.  
  112. void CMyFormView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
  113. {
  114.     // Obtain a pointer to the document object.
  115.     // Note that OnDraw isn't used, since controls in a dialog
  116.     // box can paint themselves.
  117.     CFormDoc * pDoc = (CFormDoc *)GetDocument();
  118.  
  119.     // Set the dialog class's member variables to those in the document.
  120.     m_dlgPhrase = pDoc->GetPhrase();
  121.     m_blue = GetBValue(pDoc->GetColor()) == 255;
  122.     m_green = GetGValue(pDoc->GetColor()) == 255;
  123.     m_red = GetRValue(pDoc->GetColor()) == 255;
  124.     
  125.     // Cause DDX to occur from dialog object to dialog box.
  126.     UpdateData(FALSE);
  127. }
  128.  
  129. void CMyFormView::OnInitialUpdate() 
  130. {
  131.     CFormView::OnInitialUpdate();
  132.     
  133.     // These next 2 calls make the main frame exactly the size
  134.     // of form object.
  135.     GetParentFrame()->RecalcLayout();
  136.     ResizeParentToFit();
  137. }
  138.  
  139. void CMyFormView::OnChangeWelcome() 
  140. {
  141.     // Cause DDX to occur from dialog box to dialog object.
  142.     UpdateData(TRUE);
  143.  
  144.     // Set the document's member variables to those in the dialog object.
  145.     CFormDoc * pDoc = (CFormDoc *)GetDocument();
  146.     pDoc->SetPhrase(m_dlgPhrase);
  147. }
  148.  
  149. void CMyFormView::OnColor(UINT nID) 
  150. {
  151.     // Cause DDX to occur from dialog box to dialog object.
  152.     UpdateData(TRUE);
  153.  
  154.     // Determine the state of the color check boxes, build an RGB
  155.     // value, and store it back into the document.
  156.     int r, g, b;
  157.     r = m_red ? 255 : 0;
  158.     g = m_green ? 255 : 0;
  159.     b = m_blue ? 255 : 0;
  160.  
  161.     CFormDoc * pDoc = (CFormDoc *)GetDocument();
  162.     pDoc->SetColor(RGB(r,g,b));
  163.  
  164.     // Now, invalidate the edit control so it repaints itself.
  165.     CWnd * pWnd = GetDlgItem(IDC_WELCOME);
  166.     pWnd->Invalidate();
  167. }
  168.  
  169. HBRUSH CMyFormView::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
  170. {
  171.     HBRUSH hbr = CFormView::OnCtlColor(pDC, pWnd, nCtlColor);
  172.     
  173.     // Note that this function is called before all controls on
  174.     // the form are painted. The intent is to only change the
  175.     // color of the edit box, so it's necessary to clarify for
  176.     // which control this function is being called.
  177.     if (GetDlgItem(IDC_WELCOME)->m_hWnd == pWnd->m_hWnd)
  178.     {
  179.         CFormDoc * pDoc = (CFormDoc *)GetDocument();
  180.         pDC->SetTextColor(pDoc->GetColor());
  181.     }
  182.     
  183.     return hbr;
  184. }
  185.