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

  1. // SnapView.cpp : implementation of the CSnapView class
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include "stdafx.h"
  14. #include "SnapVw.h"
  15.  
  16. #include "SnapDoc.h"
  17. #include "SnapView.h"
  18. #include "BkfstPg.h"
  19. #include "LunchPg.h"
  20. #include "DinnerPg.h"
  21. #include "SnapPs.h"
  22.  
  23.  
  24. #ifdef _DEBUG
  25. #define new DEBUG_NEW
  26. #undef THIS_FILE
  27. static char THIS_FILE[] = __FILE__;
  28. #endif
  29.  
  30. /////////////////////////////////////////////////////////////////////////////
  31. // CSnapView
  32.  
  33. IMPLEMENT_DYNCREATE(CSnapView, CFormView)
  34.  
  35. BEGIN_MESSAGE_MAP(CSnapView, CFormView)
  36.     //{{AFX_MSG_MAP(CSnapView)
  37.     ON_WM_SIZE()
  38.     //}}AFX_MSG_MAP
  39.     // Standard printing commands
  40.     ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
  41.     ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
  42.     ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
  43.     ON_WM_ERASEBKGND()
  44. END_MESSAGE_MAP()
  45.  
  46. /////////////////////////////////////////////////////////////////////////////
  47. // CSnapView construction/destruction
  48.  
  49. CSnapView::CSnapView() : CFormView((LPCTSTR)NULL)
  50. {
  51.     //{{AFX_DATA_INIT(CSnapView)
  52.         // NOTE: the ClassWizard will add member initialization here
  53.     //}}AFX_DATA_INIT
  54.     // TODO: add construction code here
  55.  
  56.     m_bSizedBefore = FALSE;
  57.     m_pPropSheet = NULL;
  58.     m_pPageBkfst = NULL;
  59.     m_pPageLunch = NULL;
  60.     m_pPageDinner = NULL;
  61.  
  62.     m_hWndFocus = NULL;
  63. }
  64.  
  65. CSnapView::~CSnapView()
  66. {
  67.     // explicitly delete sheet and pages
  68.     delete m_pPropSheet;
  69.     delete m_pPageBkfst;
  70.     delete m_pPageLunch;
  71.     delete m_pPageDinner;
  72. }
  73.  
  74. void CSnapView::DoDataExchange(CDataExchange* pDX)
  75. {
  76.     CFormView::DoDataExchange(pDX);
  77.     //{{AFX_DATA_MAP(CSnapView)
  78.         // NOTE: the ClassWizard will add DDX and DDV calls here
  79.     //}}AFX_DATA_MAP
  80. }
  81.  
  82. /////////////////////////////////////////////////////////////////////////////
  83. // CSnapView printing
  84.  
  85.  
  86. /////////////////////////////////////////////////////////////////////////////
  87. // CSnapView diagnostics
  88.  
  89. #ifdef _DEBUG
  90. void CSnapView::AssertValid() const
  91. {
  92.     CFormView::AssertValid();
  93. }
  94.  
  95. void CSnapView::Dump(CDumpContext& dc) const
  96. {
  97.     CFormView::Dump(dc);
  98. }
  99.  
  100. CSnapDoc* CSnapView::GetDocument() // non-debug version is inline
  101. {
  102.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CSnapDoc)));
  103.     return (CSnapDoc*) m_pDocument;
  104. }
  105. #endif //_DEBUG
  106.  
  107. /////////////////////////////////////////////////////////////////////////////
  108. // CSnapView message handlers
  109.  
  110. void CSnapView::OnSize(UINT nType, int cx, int cy)
  111. {
  112.     if (nType != SIZE_MINIMIZED && cx != 0 && cy != 0 && m_pPropSheet != NULL)
  113.     {
  114.         if (m_bSizedBefore == FALSE)
  115.         {
  116.             m_bSizedBefore = TRUE;
  117.  
  118.             // get the size of the property sheet
  119.             CRect rectSized;
  120.             m_pPropSheet->GetWindowRect(rectSized);
  121.  
  122.             // calculate the size of the frame
  123.             CFrameWnd* pFrame = GetParentFrame();
  124.             if (pFrame != NULL)
  125.             {
  126.                 pFrame->CalcWindowRect(rectSized);
  127.                 CWnd* pParent = pFrame->GetParent();
  128.  
  129.                 if (pParent != NULL)
  130.                     pParent->ScreenToClient(rectSized);
  131.  
  132.                 // resize and reposition the frame
  133.                 pFrame->MoveWindow(rectSized);
  134.             }
  135.         }
  136.     }
  137. }
  138.  
  139. BOOL CSnapView::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName,
  140.     DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
  141. {
  142.     ASSERT(pParentWnd != NULL);
  143.     ASSERT_KINDOF(CFrameWnd, pParentWnd);
  144.  
  145.     if (!CWnd::Create(lpszClassName, lpszWindowName, dwStyle | WS_CLIPCHILDREN,
  146.         rect, pParentWnd, nID, pContext))
  147.     {
  148.         return FALSE;
  149.     }
  150.  
  151.     // add your pages here!
  152.  
  153.     m_pPageBkfst = new CBkfstPage;
  154.     m_pPageLunch = new CLunchPage;
  155.     m_pPageDinner = new CDinnerPage;
  156.  
  157.     // create the window object
  158.  
  159.     m_pPropSheet = new CSnapPropertySheet;
  160.     m_pPropSheet->AddPage(m_pPageBkfst);
  161.     m_pPropSheet->AddPage(m_pPageLunch);
  162.     m_pPropSheet->AddPage(m_pPageDinner);
  163.  
  164.     // create a modeless property page
  165.     if (!m_pPropSheet->Create(this,
  166.             DS_CONTEXTHELP | DS_SETFONT | WS_CHILD | WS_VISIBLE))
  167.     {
  168.         DestroyWindow();
  169.         return FALSE;
  170.     }
  171.  
  172.     m_pPropSheet->SetWindowPos(NULL, 0, 0, 0, 0,
  173.             SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOSIZE);
  174.  
  175.     // we use the style from the template - but make sure that
  176.     // the WS_BORDER bit is correct.
  177.     // the WS_BORDER bit will be whatever is in dwRequestedStyle
  178.  
  179.     m_pPropSheet->ModifyStyle(WS_BORDER|WS_CAPTION,
  180.         dwStyle & (WS_BORDER|WS_CAPTION));
  181.  
  182.     // Force the size requested.
  183.     // Fake a call to OnSize()--it would have been called automatically
  184.     // if this were using the base class implementation of Create().
  185.  
  186.     CFrameWnd* pParentFrame = GetParentFrame();
  187.     CRect rectSize;
  188.     m_pPropSheet->GetWindowRect(rectSize);
  189.     pParentFrame->CalcWindowRect(rectSize);
  190.     OnSize(SIZE_RESTORED, rectSize.Width(), rectSize.Height());
  191.  
  192.     return TRUE;
  193. }
  194.  
  195. BOOL CSnapView::OnEraseBkgnd(CDC* pDC)
  196. {
  197.     CBrush* pOldBrush;
  198.  
  199.     CBrush backBrush;
  200.     backBrush.CreateSolidBrush(::GetSysColor(COLOR_3DLIGHT));
  201.     backBrush.UnrealizeObject();
  202.  
  203.     CRect rectClient;
  204.     GetClientRect(rectClient);
  205.  
  206.     pOldBrush = pDC->SelectObject(&backBrush);
  207.     pDC->Rectangle(rectClient);
  208.  
  209.     pDC->SelectObject(pOldBrush);
  210.     return TRUE;
  211. }
  212.  
  213. void CSnapView::OnDraw(CDC* pDC)
  214. {
  215.     // we don't draw because the child window will do it all
  216. }
  217.  
  218. void CSnapView::OnInitialUpdate()
  219. {
  220.     CFormView::OnInitialUpdate();
  221.  
  222.     SetScaleToFitSize(CSize(1, 1));
  223. }
  224.