home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / WORDPAD.PAK / COLORLIS.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  3.6 KB  |  154 lines

  1. // colorlis.cpp : implementation file
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1995 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 "wordpad.h"
  15. #include "colorlis.h"
  16.  
  17. #ifdef _DEBUG
  18. #undef THIS_FILE
  19. static char BASED_CODE THIS_FILE[] = __FILE__;
  20. #endif
  21.  
  22. /////////////////////////////////////////////////////////////////////////////
  23. // CColorMenu
  24.  
  25. /*
  26. int BASED_CODE CColorMenu::indexMap[17] = {
  27.     0,         //black
  28.     19,     //white
  29.     13,     //red
  30.     14,     //green
  31.     16,     //blue
  32.     15,     //yellow
  33.     17,     //magenta
  34.     18,     //cyan
  35.     1,         //dark red
  36.     2,         //dark green
  37.     4,         //dark blue
  38.     3,         //light brown
  39.     5,         //purple
  40.     6,         //dark cyan
  41.     7,         //light gray
  42.     12,     //gray
  43.     0};        //automatic
  44. */
  45.  
  46. int BASED_CODE CColorMenu::indexMap[17] = {
  47.     0,         //black
  48.     1,         //dark red
  49.     2,         //dark green
  50.     3,         //light brown
  51.     4,         //dark blue
  52.     5,         //purple
  53.     6,         //dark cyan
  54.     12,     //gray
  55.     7,         //light gray
  56.     13,     //red
  57.     14,     //green
  58.     15,     //yellow
  59.     16,     //blue
  60.     17,     //magenta
  61.     18,     //cyan
  62.     19,     //white
  63.     0};        //automatic
  64.  
  65. CColorMenu::CColorMenu()
  66. {
  67.     VERIFY(CreatePopupMenu());
  68.     ASSERT(GetMenuItemCount()==0);
  69.     for (int i=0; i<=16;i++)
  70.         VERIFY(AppendMenu(MF_OWNERDRAW, ID_COLOR0+i, (LPCTSTR)(ID_COLOR0+i)));
  71. }
  72.  
  73. COLORREF CColorMenu::GetColor(UINT id)
  74. {
  75.     ASSERT(id >= ID_COLOR0);
  76.     ASSERT(id <= ID_COLOR16);
  77.     if (id == ID_COLOR16) // autocolor
  78.         return ::GetSysColor(COLOR_WINDOWTEXT);
  79.     else
  80.     {
  81.         CPalette* pPal = CPalette::FromHandle( (HPALETTE) GetStockObject(DEFAULT_PALETTE));
  82.         ASSERT(pPal != NULL);
  83.         PALETTEENTRY pe;
  84.         if (pPal->GetPaletteEntries(indexMap[id-ID_COLOR0], 1, &pe) == 0)
  85.             return ::GetSysColor(COLOR_WINDOWTEXT);
  86.         else
  87.             return RGB(pe.peRed,pe.peGreen,pe.peBlue);
  88. //        return PALETTEINDEX(CColorMenu::indexMap[id-ID_COLOR0]);
  89.     }
  90. }
  91.  
  92. void CColorMenu::DrawItem(LPDRAWITEMSTRUCT lpDIS)
  93. {
  94.     ASSERT(lpDIS->CtlType == ODT_MENU);
  95.     UINT id = (UINT)(WORD)lpDIS->itemID;
  96.     ASSERT(id == lpDIS->itemData);
  97.     ASSERT(id >= ID_COLOR0);
  98.     ASSERT(id <= ID_COLOR16);
  99.     CDC dc;
  100.     dc.Attach(lpDIS->hDC);
  101.  
  102.     CRect rc(lpDIS->rcItem);
  103.     ASSERT(rc.Width() < 500);
  104.     if (lpDIS->itemState & ODS_FOCUS)
  105.         dc.DrawFocusRect(&rc);
  106.  
  107.     COLORREF cr = (lpDIS->itemState & ODS_SELECTED) ? 
  108.         ::GetSysColor(COLOR_HIGHLIGHT) :
  109.         dc.GetBkColor();
  110.  
  111.     CBrush brushFill(cr);
  112.     cr = dc.GetTextColor();
  113.  
  114.     if (lpDIS->itemState & ODS_SELECTED)
  115.         dc.SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));
  116.  
  117.     int nBkMode = dc.SetBkMode(TRANSPARENT);
  118.     dc.FillRect(&rc, &brushFill);
  119.  
  120.     rc.left += 50;
  121.     CString strColor;
  122.     strColor.LoadString(id);
  123.     dc.TextOut(rc.left,rc.top,strColor,strColor.GetLength());
  124.     rc.left -= 45;
  125.     rc.top += 2;
  126.     rc.bottom -= 2;
  127.     rc.right = rc.left + 40;
  128.     CBrush brush(GetColor(id));
  129.     CBrush* pOldBrush = dc.SelectObject(&brush);
  130.     dc.Rectangle(rc);
  131.  
  132.     dc.SelectObject(pOldBrush);
  133.     dc.SetTextColor(cr);
  134.     dc.SetBkMode(nBkMode);
  135.     
  136.     dc.Detach();
  137. }
  138.  
  139. void CColorMenu::MeasureItem(LPMEASUREITEMSTRUCT lpMIS)
  140. {
  141.     ASSERT(lpMIS->CtlType == ODT_MENU);
  142.     UINT id = (UINT)(WORD)lpMIS->itemID;
  143.     ASSERT(id == lpMIS->itemData);
  144.     ASSERT(id >= ID_COLOR0);
  145.     ASSERT(id <= ID_COLOR16);
  146.     CDisplayIC dc;
  147.     CString strColor;
  148.     strColor.LoadString(id);
  149.     CSize sizeText = dc.GetTextExtent(strColor,strColor.GetLength());
  150.     ASSERT(sizeText.cx < 500);
  151.     lpMIS->itemWidth = sizeText.cx + 50;
  152.     lpMIS->itemHeight = sizeText.cy;
  153. }
  154.