home *** CD-ROM | disk | FTP | other *** search
/ Mastering MFC Development / MMD.ISO / samples / c05 / owndraw / odrawmnu.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-20  |  2.3 KB  |  92 lines

  1. // ODrawMnu.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "owndraw.h"
  6. #include "ODrawMnu.h"
  7.  
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13.  
  14. /////////////////////////////////////////////////////////////////////////////
  15. // CODmenus
  16.  
  17. CODmenus::CODmenus()
  18. {
  19.     VERIFY(CreateMenu());
  20. }
  21.  
  22. CODmenus::~CODmenus()
  23. {
  24.     Detach();
  25.     ASSERT(m_hMenu == NULL);
  26.     // default CMenu::~CMenu will destroy
  27. }
  28.  
  29. void CODmenus::MeasureItem(LPMEASUREITEMSTRUCT lpMIS)
  30. {
  31.     // Just to show how this function can differentiate which
  32.     // menu item it's drawing, we'll make each menu item be taller
  33.     // than its predecessor
  34.     switch (lpMIS->itemID)
  35.     {
  36.         case IDR_COLOR_BLUE:
  37.             lpMIS->itemHeight = 2 * COLOR_BOX_HEIGHT;
  38.             break;
  39.         case IDR_COLOR_GREEN:
  40.             lpMIS->itemHeight = 4 * COLOR_BOX_HEIGHT;
  41.             break;
  42.         case IDR_COLOR_RED:
  43.             lpMIS->itemHeight = 8 * COLOR_BOX_HEIGHT;
  44.             break;
  45.         default:
  46.             lpMIS->itemHeight = COLOR_BOX_HEIGHT;
  47.     }
  48.     lpMIS->itemWidth = COLOR_BOX_WIDTH;
  49. }
  50.  
  51. void CODmenus::DrawItem(LPDRAWITEMSTRUCT lpDIS)
  52. {
  53.     CDC* pDC = CDC::FromHandle(lpDIS->hDC);
  54.     
  55.     // RGB in item data
  56.     COLORREF cr = (COLORREF)lpDIS->itemData;
  57.  
  58.     // For each menu item, there are possibly three things to
  59.     // do. First, we have to determine if the entire item needs
  60.     // to be drawn. This is simply a matter of selecting a brush
  61.     // and filling the rectangle
  62.     if (lpDIS->itemAction & ODA_DRAWENTIRE)
  63.     {
  64.         CBrush br(cr);
  65.         pDC->FillRect(&lpDIS->rcItem, &br);
  66.     }
  67.  
  68.     // Next, if an item's selection state has changed, we
  69.     // determine if it has changed to selected and if it has
  70.     // we need to hilite its frame. By also checking for
  71.     // ODA_DRAWENTIRE, we force the first item in the menu to
  72.     // be selected when it is drawn. 
  73.     if ((lpDIS->itemAction & (ODA_SELECT | ODA_DRAWENTIRE))
  74.         && (lpDIS->itemState & ODS_SELECTED))
  75.     {
  76.         COLORREF crHilite = RGB(255-GetRValue(cr),
  77.                         255-GetGValue(cr), 255-GetBValue(cr));
  78.         CBrush br(crHilite);
  79.         pDC->FrameRect(&lpDIS->rcItem, &br);
  80.     }
  81.  
  82.     // Finally, if an item's selection state has changed and
  83.     // that change is to NOT selected, we need to remove the
  84.     // hiliting frame.
  85.     if ( (lpDIS->itemAction & ODA_SELECT) &&
  86.         !(lpDIS->itemState & ODS_SELECTED))
  87.     {
  88.         CBrush br(cr);
  89.         pDC->FrameRect(&lpDIS->rcItem, &br);
  90.     }
  91. }
  92.