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 / 22324 next >
Encoding:
Text File  |  1998-06-18  |  4.3 KB  |  126 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 combo 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$$, CComboBox)
  31.     //{{AFX_MSG_MAP($$VAL:ClassName$$)
  32.     //}}AFX_MSG_MAP
  33. END_MESSAGE_MAP()
  34.  
  35.  
  36. /////////////////////////////////////////////////////////////////////////////
  37. // $$VAL:ClassName$$ message handlers
  38.  
  39. // This function compares two colors to each other. If you're doing 
  40. // something different, like comparing two bitmaps to each other, use 
  41. // a different algorithm.  If you are working with strings, the default 
  42. // implementation provided by MFC will probably do the job just fine.  
  43. // (Example of a string where you need to do your own comparisons: if 
  44. // your sorting scheme is different than a standard collating sequence, 
  45. // such as one where the comparisons need to be case-insensitive.)
  46. int $$VAL:ClassName$$::CompareItem(LPCOMPAREITEMSTRUCT lpCompareItemStruct)
  47. {
  48.     COLORREF color1 = (COLORREF)lpCompareItemStruct->itemData1;
  49.     COLORREF color2 = (COLORREF)lpCompareItemStruct->itemData2;
  50.     if (color1 == color2)
  51.         return 0;       // exact match
  52.  
  53.     // first do an intensity sort, lower intensities go first
  54.     int intensity1 = GetRValue(color1) + GetGValue(color1) + GetBValue(color1);
  55.     int intensity2 = GetRValue(color2) + GetGValue(color2) + GetBValue(color2);
  56.     if (intensity1 < intensity2)
  57.         return -1;      // lower intensity goes first
  58.     else if (intensity1 > intensity2)
  59.         return 1;       // higher intensity goes second
  60.  
  61.     // if same intensity, sort by color (blues first, reds last)
  62.     if (GetBValue(color1) > GetBValue(color2))
  63.         return -1;
  64.     else if (GetGValue(color1) > GetGValue(color2))
  65.         return -1;
  66.     else if (GetRValue(color1) > GetRValue(color2))
  67.         return -1;
  68.     else
  69.         return 1;
  70. }
  71.  
  72. // This function draws a color bar in the client space of this combo box 
  73. // item.  If your combo box item contains something other than color bars, 
  74. // you need to replace the contents of this function with your own code 
  75. // for drawing it.  If you are working with strings, the default MFC
  76. // implementation of this function will probably work just fine for you.  
  77. // (Example of a string where you need to do the drawing yourself: one 
  78. // displayed in a different font than the rest of the dialog.)
  79. void $$VAL:ClassName$$::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
  80. {
  81.     CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
  82.     COLORREF color = (COLORREF)lpDrawItemStruct->itemData; // RGB in item data
  83.  
  84.     // White space
  85.     CRect rcItem(lpDrawItemStruct->rcItem);
  86.     rcItem.InflateRect(-2, -2);
  87.  
  88.     // Paint the color item in the color requested
  89.     CBrush brush(color);
  90.     pDC->FillRect(rcItem, &brush);
  91.  
  92.     // Focus rect
  93.     if (lpDrawItemStruct->itemAction & ODA_FOCUS)
  94.         pDC->DrawFocusRect(&lpDrawItemStruct->rcItem);
  95. }
  96.  
  97. #define COLOR_ITEM_HEIGHT   20
  98.  
  99. // We made our color bars all be the same height for simplicity.  You 
  100. // can actually specify variable heights as long as you set the 
  101. // CBS_OWNERDRAWVARIABLE style.
  102. void $$VAL:ClassName$$::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
  103. {
  104.     // all items are of fixed size
  105.     // must use CBS_OWNERDRAWVARIABLE for this to work
  106.     lpMeasureItemStruct->itemHeight = COLOR_ITEM_HEIGHT;
  107. }
  108.  
  109. void $$VAL:ClassName$$::AddListItem(COLORREF color)
  110. {
  111.     // add a combo box item
  112.     AddString((LPCTSTR) color);
  113.         // Combo box does not have the CBS_HASSTRINGS style, so the
  114.         //  normal combo box string is used to store an RGB color
  115. }
  116.  
  117. // add 8 colors to the combo box (primary + secondary color only)
  118. void $$VAL:ClassName$$::LoadList()
  119. {
  120.     for (int red = 0; red <= 255; red += 255)
  121.         for (int green = 0; green <= 255; green += 255)
  122.             for (int blue = 0; blue <= 255; blue += 255)
  123.                 AddListItem(RGB(red, green, blue));
  124. }
  125.  
  126.