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

  1. // ListView.cpp : implementation of the CMyListView class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "List.h"
  6.  
  7. #include "ListDoc.h"
  8. #include "ListView.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. // CMyListView
  18.  
  19. IMPLEMENT_DYNCREATE(CMyListView, CListView)
  20.  
  21. BEGIN_MESSAGE_MAP(CMyListView, CListView)
  22.     //{{AFX_MSG_MAP(CMyListView)
  23.         // NOTE - the ClassWizard will add and remove mapping macros here.
  24.         //    DO NOT EDIT what you see in these blocks of generated code!
  25.     //}}AFX_MSG_MAP
  26. END_MESSAGE_MAP()
  27.  
  28. /////////////////////////////////////////////////////////////////////////////
  29. // CMyListView construction/destruction
  30.  
  31. CMyListView::CMyListView()
  32. {
  33. }
  34.  
  35. CMyListView::~CMyListView()
  36. {
  37. }
  38.  
  39. BOOL CMyListView::PreCreateWindow(CREATESTRUCT& cs)
  40. {
  41.     return CListView::PreCreateWindow(cs);
  42. }
  43.  
  44. /////////////////////////////////////////////////////////////////////////////
  45. // CMyListView drawing
  46.  
  47. void CMyListView::OnDraw(CDC* pDC)
  48. {
  49.     CListDoc* pDoc = GetDocument();
  50.     ASSERT_VALID(pDoc);
  51. }
  52.  
  53. void CMyListView::OnInitialUpdate()
  54. {
  55.     int i;
  56.     CString s;
  57.     LV_COLUMN lv;
  58.     CListCtrl & clc = GetListCtrl();
  59.  
  60.     clc.ModifyStyle(NULL, LVS_REPORT);
  61.  
  62.     lv.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
  63.     lv.fmt = LVCFMT_LEFT;
  64.     
  65.     // Create 4 columns in the list control and
  66.     // give each column a header string.
  67.     for (i = ID_COLUMN1; i <= ID_COLUMN4; i++)
  68.     {
  69.         s.LoadString(i);
  70.         lv.cx = 15 * clc.GetStringWidth(s) / 10;
  71.         lv.pszText = (char *) (const char *)s;
  72.         clc.InsertColumn(i - ID_COLUMN1, &lv);
  73.         lv.fmt = LVCFMT_CENTER;
  74.     }
  75.  
  76.     CListView::OnInitialUpdate();
  77. }
  78.  
  79. /////////////////////////////////////////////////////////////////////////////
  80. // CMyListView diagnostics
  81.  
  82. #ifdef _DEBUG
  83. void CMyListView::AssertValid() const
  84. {
  85.     CListView::AssertValid();
  86. }
  87.  
  88. void CMyListView::Dump(CDumpContext& dc) const
  89. {
  90.     CListView::Dump(dc);
  91. }
  92.  
  93. CListDoc* CMyListView::GetDocument() // non-debug version is inline
  94. {
  95.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CListDoc)));
  96.     return (CListDoc*)m_pDocument;
  97. }
  98. #endif //_DEBUG
  99.  
  100. /////////////////////////////////////////////////////////////////////////////
  101. // CMyListView message handlers
  102.  
  103. void CMyListView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
  104. {
  105.     CListCtrl & clc = GetListCtrl();
  106.     CString s;
  107.  
  108.     // Remove all the list's data. Since OnUpdate will be called
  109.     // when the list's data changes, this call is necessary before
  110.     // rebuilding the list.
  111.     clc.DeleteAllItems();
  112.  
  113.     // Each iteration of the loop builds 1 row. InsertItem fills
  114.     // column 0, SetItemText fills the other columns.
  115.     for (int i = ID_BLACK; i <= ID_WHITE; i++)
  116.     {
  117.         s.LoadString(i);
  118.         // row will be 0-7, which corresponds to the 8 basic colors
  119.         int row = i-ID_BLACK;
  120.         // insert the string
  121.         clc.InsertItem(row, s);
  122.  
  123.         // convert the color to its component RGB values
  124.         int mask = 4;
  125.         for (int col = 1; col <= 3; col++)
  126.         {
  127.             // Determine if a bit is on or not.
  128.             s.Format("%d", row & mask ? 255 : 0);
  129.             clc.SetItemText(row, col, s);
  130.             mask /= 2;
  131.         }
  132.     }
  133. }
  134.