home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / advanced / chkbook / bookvw.cpp next >
Encoding:
C/C++ Source or Header  |  1998-03-27  |  4.0 KB  |  145 lines

  1. // bookvw.cpp : implementation of the CBookView 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.  
  14. #include "stdafx.h"
  15. #include "chkbook.h"
  16. #include <stdlib.h>
  17.  
  18. #ifdef _DEBUG
  19. #undef THIS_FILE
  20. static char BASED_CODE THIS_FILE[] = __FILE__;
  21. #endif
  22.  
  23. IMPLEMENT_DYNCREATE(CBookView, CRowView)
  24.  
  25. /////////////////////////////////////////////////////////////////////////////
  26. CBookView::CBookView()
  27. {
  28. }
  29.  
  30. /////////////////////////////////////////////////////////////////////////////
  31. // Overrides of CView and CRowView
  32.  
  33. void CBookView::OnUpdate(CView*, LPARAM lHint, CObject* pHint)
  34. {
  35.     // OnUpdate() is called whenever the document has changed and,
  36.     // therefore, the view needs to redisplay some or all of itself.
  37.  
  38.     if (DYNAMIC_DOWNCAST(CFixedLenRecHint, pHint) != NULL)
  39.     {
  40.         int nRow = LOWORD(lHint);
  41.         UpdateRow(nRow);
  42.     }
  43.     else
  44.     {
  45.         Invalidate();
  46.     }
  47. }
  48.  
  49. void CBookView::GetRowWidthHeight(CDC* pDC, int& nRowWidth, int& nRowHeight)
  50. {
  51.     TEXTMETRIC tm;
  52.     pDC->GetTextMetrics(&tm);
  53.     nRowWidth = tm.tmAveCharWidth * ROW_WIDTH;
  54.     nRowHeight = tm.tmHeight * 2; // 2 lines of text
  55. }
  56.  
  57. int CBookView::GetActiveRow()
  58. {
  59.     CChkBookDoc* pDoc = GetDocument();
  60.     return (pDoc->GetActiveCheckNo() - pDoc->GetFirstCheckNo());
  61. }
  62.  
  63. int CBookView::GetRowCount()
  64. {
  65.     CChkBookDoc* pDoc = GetDocument();
  66.     return (pDoc->GetLastCheckNo() - pDoc->GetFirstCheckNo() + 1);
  67. }
  68.  
  69. void CBookView::ChangeSelectionNextRow(BOOL bNext)
  70. {
  71.     GetDocument()->ChangeSelectionNextCheckNo(bNext);
  72. }
  73.  
  74. void CBookView::ChangeSelectionToRow(int nRow)
  75. {
  76.     CChkBookDoc* pDoc = GetDocument();
  77.     pDoc->ChangeSelectionToCheckNo(nRow + pDoc->GetFirstCheckNo());
  78. }
  79.  
  80. void CBookView::OnDrawRow(CDC* pDC, int nRow, int y, BOOL bSelected)
  81. {
  82.     // Prepare for highlighting or un-highlighting the check, depending
  83.     // on whether it is the currently selected check or not.  And
  84.     // paint the background (behind the text) accordingly.
  85.     CBrush brushBackground;
  86.  
  87.     // save colors for drawing selected item on the screen
  88.     COLORREF crOldText = 0;
  89.     COLORREF crOldBackground = 0;
  90.  
  91.     if (!pDC->IsPrinting())
  92.     {
  93.         if (bSelected)
  94.         {
  95.             brushBackground.CreateSolidBrush(::GetSysColor(COLOR_HIGHLIGHT));
  96.             crOldBackground = pDC->SetBkColor(::GetSysColor(COLOR_HIGHLIGHT));
  97.             crOldText = pDC->SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));
  98.         }
  99.         else
  100.         {
  101.             brushBackground.CreateSolidBrush(::GetSysColor(COLOR_WINDOW));
  102.         }
  103.  
  104.         CRect rectSelection;
  105.         pDC->GetClipBox(&rectSelection);
  106.         rectSelection.top = y;
  107.         rectSelection.bottom = y + m_nRowHeight;
  108.         pDC->FillRect(&rectSelection, &brushBackground);
  109.     }
  110.  
  111.     // Get the data for the specific check.
  112.     DWORD dwCents;
  113.     CString strDate, strPayTo, strMemo;
  114.     int nCheckNo = nRow + GetDocument()->GetFirstCheckNo();
  115.  
  116.     GetDocument()->GetCheck(nCheckNo, dwCents, strPayTo, strDate, strMemo);
  117.  
  118.     TEXTMETRIC tm;
  119.     pDC->GetTextMetrics(&tm);
  120.  
  121.     // Display the check in two lines of text.
  122.     TCHAR num[10];
  123.     // line 1
  124.     wsprintf(num, _T("%u"), nCheckNo);
  125.     pDC->TextOut(CHECKNO_COL*tm.tmAveCharWidth, y, num, _tcslen(num));
  126.     pDC->TextOut(DATE_COL*tm.tmAveCharWidth, y, strDate);
  127.     pDC->TextOut(PAYTO_COL*tm.tmAveCharWidth, y, strPayTo);
  128.     CString strDollarCents;
  129.     strDollarCents = GetDollarsCentsFormatted(dwCents);
  130.     // right justify the check dollar/cent amount
  131.     pDC->TextOut(
  132.         (AMOUNT_COL + AMOUNT_LEN - strDollarCents.GetLength()) * tm.tmAveCharWidth,
  133.         y,
  134.         strDollarCents);
  135.     // line 2
  136.     pDC->TextOut(MEMO_COL*tm.tmAveCharWidth, y + tm.tmHeight, strMemo);
  137.  
  138.     // Restore the DC.
  139.     if (!pDC->IsPrinting() && bSelected)
  140.     {
  141.         pDC->SetBkColor(crOldBackground);
  142.         pDC->SetTextColor(crOldText);
  143.     }
  144. }
  145.