home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / common / msdev98 / bin / ide / devgal.pkg / TEMPLATE / 22326 < prev    next >
Encoding:
Text File  |  1998-06-18  |  4.5 KB  |  130 lines

  1. // $$VAL:CppFile$$ : implementation file
  2. // See $$VAL:HeaderFile$$ for details on how to use this class
  3. //
  4.  
  5. #include "stdafx.h"
  6. #include "$$VAL:HeaderFile$$"
  7.  
  8. #ifdef _DEBUG
  9. #undef THIS_FILE
  10. static char BASED_CODE THIS_FILE[] = __FILE__;
  11. #endif
  12.  
  13. /////////////////////////////////////////////////////////////////////////////
  14. // $$VAL:ClassName$$
  15.  
  16. $$VAL:ClassName$$::$$VAL:ClassName$$()
  17. {
  18.     // You can't actually add anything to the list box here since the
  19.     // associated CWnd hasn't been created yet.  Save any initialization
  20.     // you need to do for after the CWnd has been constructed.  The TODO: 
  21.     // comments in $$VAL:HeaderFile$$ explain how to do this for 
  22.     // $$VAL:ClassName$$ as implemented here.
  23. }
  24.  
  25. $$VAL:ClassName$$::~$$VAL:ClassName$$()
  26. {
  27. }
  28.  
  29.  
  30. BEGIN_MESSAGE_MAP($$VAL:ClassName$$, CListBox)
  31.     //{{AFX_MSG_MAP($$VAL:ClassName$$)
  32.         // NOTE - the ClassWizard will add and remove mapping macros here.
  33.     //}}AFX_MSG_MAP
  34. END_MESSAGE_MAP()
  35.  
  36.  
  37. /////////////////////////////////////////////////////////////////////////////
  38. // $$VAL:ClassName$$ message handlers
  39.  
  40. // This function draws a color bar in the client space of this list box 
  41. // item.  If your list box item contains something other than color bars, 
  42. // you need to replace the contents of this function with your own code 
  43. // for drawing it.  If you are working with strings, the default MFC
  44. // implementation of this function will probably work just fine for you.  
  45. // (Example of a string where you need to do the drawing yourself: one 
  46. // displayed in a different font than the rest of the dialog.)
  47. void $$VAL:ClassName$$::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
  48. {
  49.     CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
  50.     COLORREF color = (COLORREF)lpDrawItemStruct->itemData; // RGB in item data
  51.  
  52.     // White space
  53.     CRect rcItem(lpDrawItemStruct->rcItem);
  54.     rcItem.InflateRect(-2, -2);
  55.  
  56.     // Paint the color item in the color requested
  57.     CBrush brush(color);
  58.     pDC->FillRect(rcItem, &brush);
  59.  
  60.     // Focus rect
  61.     if (lpDrawItemStruct->itemAction & ODA_FOCUS)
  62.         pDC->DrawFocusRect(&lpDrawItemStruct->rcItem);
  63. }
  64.  
  65. // This function compares two colors to each other. If you're doing 
  66. // something different, like comparing two bitmaps to each other, use 
  67. // a different algorithm.  If you are working with strings, the default 
  68. // implementation provided by MFC will probably do the job just fine.  
  69. // (Example of a string where you need to do your own comparisons: if 
  70. // your sorting scheme is different than a standard collating sequence, 
  71. // such as one where the comparisons need to be case-insensitive.)
  72. int $$VAL:ClassName$$::CompareItem(LPCOMPAREITEMSTRUCT lpCompareItemStruct) 
  73. {
  74.     // return -1 = item 1 sorts before item 2
  75.     // return 0 = item 1 and item 2 sort the same
  76.     // return 1 = item 1 sorts after item 2
  77.  
  78.     COLORREF color1 = (COLORREF)lpCompareItemStruct->itemData1;
  79.     COLORREF color2 = (COLORREF)lpCompareItemStruct->itemData2;
  80.     if (color1 == color2)
  81.         return 0;       // exact match
  82.  
  83.     // first do an intensity sort, lower intensities go first
  84.     int intensity1 = GetRValue(color1) + GetGValue(color1) + GetBValue(color1);
  85.     int intensity2 = GetRValue(color2) + GetGValue(color2) + GetBValue(color2);
  86.     if (intensity1 < intensity2)
  87.         return -1;      // lower intensity goes first
  88.     else if (intensity1 > intensity2)
  89.         return 1;       // higher intensity goes second
  90.  
  91.     // if same intensity, sort by color (blues first, reds last)
  92.     if (GetBValue(color1) > GetBValue(color2))
  93.         return -1;
  94.     else if (GetGValue(color1) > GetGValue(color2))
  95.         return -1;
  96.     else if (GetRValue(color1) > GetRValue(color2))
  97.         return -1;
  98.     else
  99.         return 1;
  100. }
  101.  
  102. #define COLOR_ITEM_HEIGHT   20
  103.  
  104. // We made our color bars all be the same height for simplicity.  You 
  105. // can actually specify variable heights as long as you set the 
  106. // CBS_OWNERDRAWVARIABLE style.
  107. void $$VAL:ClassName$$::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
  108. {
  109.     // all items are of fixed size
  110.     // must use LBS_OWNERDRAWVARIABLE for this to work
  111.     lpMeasureItemStruct->itemHeight = COLOR_ITEM_HEIGHT;
  112. }
  113.  
  114. void $$VAL:ClassName$$::AddListItem(COLORREF color)
  115. {
  116.     // add a listbox item
  117.     AddString((LPCTSTR) color);
  118.         // List box does not have the LBS_HASSTRINGS style, so the
  119.         //  normal list box string is used to store an RGB color
  120. }
  121.  
  122. // add 8 colors to the list box (primary + secondary color only)
  123. void $$VAL:ClassName$$::LoadList()
  124. {
  125.     for (int red = 0; red <= 255; red += 255)
  126.         for (int green = 0; green <= 255; green += 255)
  127.             for (int blue = 0; blue <= 255; blue += 255)
  128.                 AddListItem(RGB(red, green, blue));
  129. }
  130.