home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c031 / 11.ddi / MFC / SAMPLES / CTRLTEST / CUSTLIST.CP$ / custlist
Encoding:
Text File  |  1992-03-19  |  4.6 KB  |  173 lines

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