home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / general / ctrltest / custlist.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-27  |  4.8 KB  |  184 lines

  1. // custlist.cpp : custom listbox
  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 "ctrltest.h"
  15.  
  16. /////////////////////////////////////////////////////////////////////////////
  17. // Custom Listbox - containing colors
  18.  
  19. class CColorListBox : public CListBox
  20. {
  21. public:
  22. // Operations
  23.     void AddColorItem(COLORREF color);
  24.  
  25. // Implementation
  26.     virtual void MeasureItem(LPMEASUREITEMSTRUCT lpMIS);
  27.     virtual void DrawItem(LPDRAWITEMSTRUCT lpDIS);
  28.     virtual int CompareItem(LPCOMPAREITEMSTRUCT lpCIS);
  29. };
  30.  
  31. void CColorListBox::AddColorItem(COLORREF color)
  32. {
  33.     // add a listbox item
  34.     AddString((LPCTSTR) color);
  35.         // Listbox does not have the LBS_HASSTRINGS style, so the
  36.         //  normal listbox string is used to store an RGB color
  37. }
  38.  
  39. /////////////////////////////////////////////////////////////////////////////
  40.  
  41. #define COLOR_ITEM_HEIGHT   20
  42.  
  43. void CColorListBox::MeasureItem(LPMEASUREITEMSTRUCT lpMIS)
  44. {
  45.     // all items are of fixed size
  46.     // must use LBS_OWNERDRAWVARIABLE for this to work
  47.     lpMIS->itemHeight = COLOR_ITEM_HEIGHT;
  48. }
  49.  
  50. void CColorListBox::DrawItem(LPDRAWITEMSTRUCT lpDIS)
  51. {
  52.     CDC* pDC = CDC::FromHandle(lpDIS->hDC);
  53.     COLORREF cr = (COLORREF)lpDIS->itemData; // RGB in item data
  54.  
  55.     if (lpDIS->itemAction & ODA_DRAWENTIRE)
  56.     {
  57.         // Paint the color item in the color requested
  58.         CBrush br(cr);
  59.         pDC->FillRect(&lpDIS->rcItem, &br);
  60.     }
  61.  
  62.     if ((lpDIS->itemState & ODS_SELECTED) &&
  63.         (lpDIS->itemAction & (ODA_SELECT | ODA_DRAWENTIRE)))
  64.     {
  65.         // item has been selected - hilite frame
  66.         COLORREF crHilite = RGB(255-GetRValue(cr),
  67.                         255-GetGValue(cr), 255-GetBValue(cr));
  68.         CBrush br(crHilite);
  69.         pDC->FrameRect(&lpDIS->rcItem, &br);
  70.     }
  71.  
  72.     if (!(lpDIS->itemState & ODS_SELECTED) &&
  73.         (lpDIS->itemAction & ODA_SELECT))
  74.     {
  75.         // Item has been de-selected -- remove frame
  76.         CBrush br(cr);
  77.         pDC->FrameRect(&lpDIS->rcItem, &br);
  78.     }
  79. }
  80.  
  81. int CColorListBox::CompareItem(LPCOMPAREITEMSTRUCT lpCIS)
  82. {
  83.     COLORREF cr1 = (COLORREF)lpCIS->itemData1;
  84.     COLORREF cr2 = (COLORREF)lpCIS->itemData2;
  85.     if (cr1 == cr2)
  86.         return 0;       // exact match
  87.  
  88.     // first do an intensity sort, lower intensities go first
  89.     int intensity1 = GetRValue(cr1) + GetGValue(cr1) + GetBValue(cr1);
  90.     int intensity2 = GetRValue(cr2) + GetGValue(cr2) + GetBValue(cr2);
  91.     if (intensity1 < intensity2)
  92.         return -1;      // lower intensity goes first
  93.     else if (intensity1 > intensity2)
  94.         return 1;       // higher intensity goes second
  95.  
  96.     // if same intensity, sort by color (blues first, reds last)
  97.     if (GetBValue(cr1) > GetBValue(cr2))
  98.         return -1;
  99.     else if (GetGValue(cr1) > GetGValue(cr2))
  100.         return -1;
  101.     else if (GetRValue(cr1) > GetRValue(cr2))
  102.         return -1;
  103.     else
  104.         return 1;
  105. }
  106.  
  107. /////////////////////////////////////////////////////////////////////////////
  108. // Dialog class
  109.  
  110. class CCustListDlg : public CDialog
  111. {
  112. protected:
  113.     CColorListBox  m_colors;
  114. public:
  115.     //{{AFX_DATA(CCustListDlg)
  116.         enum { IDD = IDD_CUSTOM_LIST };
  117.     //}}AFX_DATA
  118.     CCustListDlg()
  119.         : CDialog(CCustListDlg::IDD)
  120.         { }
  121.  
  122.     // access to controls is through inline helpers
  123.     BOOL OnInitDialog();
  124.  
  125.     //{{AFX_MSG(CCustListDlg)
  126.         virtual void OnOK();
  127.     //}}AFX_MSG
  128.     DECLARE_MESSAGE_MAP();
  129. };
  130.  
  131. BEGIN_MESSAGE_MAP(CCustListDlg, CDialog)
  132.     //{{AFX_MSG_MAP(CCustListDlg)
  133.     ON_LBN_DBLCLK(IDC_LISTBOX1, OnOK)       // double click for OK
  134.     //}}AFX_MSG_MAP
  135. END_MESSAGE_MAP()
  136.  
  137. BOOL CCustListDlg::OnInitDialog()
  138. {
  139.     // subclass the control
  140.     VERIFY(m_colors.SubclassDlgItem(IDC_LISTBOX1, this));
  141.  
  142.     // add 8 colors to the listbox (primary + secondary color only)
  143.     for (int red = 0; red <= 255; red += 255)
  144.         for (int green = 0; green <= 255; green += 255)
  145.             for (int blue = 0; blue <= 255; blue += 255)
  146.                 m_colors.AddColorItem(RGB(red, green, blue));
  147.  
  148.     return TRUE;
  149. }
  150.  
  151. void CCustListDlg::OnOK()
  152. {
  153.     // get the final color
  154.     int nIndex = m_colors.GetCurSel();
  155.     if (nIndex == -1)
  156.     {
  157.         CString str;
  158.         str.LoadString(IDS_PLEASE_SELECT_COLOR);
  159.         MessageBox(str);
  160.         m_colors.SetFocus();
  161.         return;
  162.     }
  163.     DWORD color = m_colors.GetItemData(nIndex);
  164.  
  165. #ifdef _DEBUG
  166.     // normally do something with it...
  167.     TRACE1("final color RGB = 0x%06lX\n", color);
  168. #endif
  169.  
  170.     EndDialog(IDOK);
  171. }
  172.  
  173. /////////////////////////////////////////////////////////////////////////////
  174. // Run the test
  175.  
  176. void CTestWindow::OnTestCustomList()
  177. {
  178.     TRACE0("running dialog with custom listbox (owner draw)\n");
  179.     CCustListDlg dlg;
  180.     dlg.DoModal();
  181. }
  182.  
  183. /////////////////////////////////////////////////////////////////////////////
  184.