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 / custmenu.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-27  |  4.4 KB  |  152 lines

  1. // custmenu.cpp : custom menu
  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.  
  18. // for owner draw menus, the CMenu object is embedded in the main frame window
  19. //  the CMenu stays attached to the HMENU while it is running so that
  20. //  owner draw messages are delegated to this class.
  21. //  Since we attach the HMENU to a menu bar (with ModifyMenu below), we
  22. //  don't want to delete the menu twice - so we detach on the destructor.
  23.  
  24. CColorMenu::CColorMenu()
  25. {
  26.     VERIFY(CreateMenu());
  27. }
  28.  
  29. CColorMenu::~CColorMenu()
  30. {
  31.     Detach();
  32.     ASSERT(m_hMenu == NULL);    // defaul CMenu::~CMenu will destroy
  33. }
  34.  
  35. void CColorMenu::AppendColorMenuItem(UINT nID, COLORREF color)
  36. {
  37.     VERIFY(AppendMenu(MF_ENABLED | MF_OWNERDRAW, nID, (LPCTSTR)color));
  38. }
  39.  
  40. /////////////////////////////////////////////////////////////////////////////
  41.  
  42. #define COLOR_BOX_WIDTH     20
  43. #define COLOR_BOX_HEIGHT    20
  44.  
  45.  
  46. void CColorMenu::MeasureItem(LPMEASUREITEMSTRUCT lpMIS)
  47. {
  48.     // all items are of fixed size
  49.     lpMIS->itemWidth = COLOR_BOX_WIDTH;
  50.     lpMIS->itemHeight = COLOR_BOX_HEIGHT;
  51. }
  52.  
  53. void CColorMenu::DrawItem(LPDRAWITEMSTRUCT lpDIS)
  54. {
  55.     CDC* pDC = CDC::FromHandle(lpDIS->hDC);
  56.     COLORREF cr = (COLORREF)lpDIS->itemData; // RGB in item data
  57.  
  58.     if (lpDIS->itemAction & ODA_DRAWENTIRE)
  59.     {
  60.         // Paint the color item in the color requested
  61.         CBrush br(cr);
  62.         pDC->FillRect(&lpDIS->rcItem, &br);
  63.     }
  64.  
  65.     if ((lpDIS->itemState & ODS_SELECTED) &&
  66.         (lpDIS->itemAction & (ODA_SELECT | ODA_DRAWENTIRE)))
  67.     {
  68.         // item has been selected - hilite frame
  69.         COLORREF crHilite = RGB(255-GetRValue(cr),
  70.                         255-GetGValue(cr), 255-GetBValue(cr));
  71.         CBrush br(crHilite);
  72.         pDC->FrameRect(&lpDIS->rcItem, &br);
  73.     }
  74.  
  75.     if (!(lpDIS->itemState & ODS_SELECTED) &&
  76.         (lpDIS->itemAction & ODA_SELECT))
  77.     {
  78.         // Item has been de-selected -- remove frame
  79.         CBrush br(cr);
  80.         pDC->FrameRect(&lpDIS->rcItem, &br);
  81.     }
  82. }
  83.  
  84. /////////////////////////////////////////////////////////////////////////////
  85. // custom menu test - menu ids: index of color.
  86.  
  87. static COLORREF colors[] = {
  88.     0x00000000,     // black
  89.     0x00FF0000,     // blue
  90.     0x0000FF00,     // green
  91.     0x00FFFF00,     // cyan
  92.     0x000000FF,     // red
  93.     0x00FF00FF,     // magenta
  94.     0x0000FFFF,     // yellow
  95.     0x00FFFFFF      // white
  96. };
  97. const int nColors = sizeof(colors)/sizeof(colors[0]);
  98.  
  99.  
  100. /////////////////////////////////////////////////////////////////////////////
  101.  
  102. // Call AttachCustomMenu once
  103. //   it will replace the menu item with the ID  'IDM_TEST_CUSTOM_MENU'
  104. //   with a color menu popup
  105. // Replace the specified menu item with a color popup
  106. void CTestWindow::AttachCustomMenu()
  107. {
  108.     // now add a few new menu items
  109.  
  110.     for (int iColor = 0; iColor < nColors; iColor++)
  111.         m_colorMenu.AppendColorMenuItem(IDS_COLOR_NAME_FIRST + iColor, colors[iColor]);
  112.  
  113.     // Replace the specified menu item with a color popup
  114.     //  (note: will only work once)
  115.     CMenu* pMenuBar = GetMenu();
  116.     ASSERT(pMenuBar != NULL);
  117.     TCHAR szString[256];     // don't change the string
  118.  
  119.     pMenuBar->GetMenuString(IDM_TEST_CUSTOM_MENU, szString, sizeof(szString),
  120.         MF_BYCOMMAND);
  121.     VERIFY(GetMenu()->ModifyMenu(IDM_TEST_CUSTOM_MENU, MF_BYCOMMAND | MF_POPUP,
  122.         (UINT)m_colorMenu.m_hMenu, szString));
  123. }
  124.  
  125. /////////////////////////////////////////////////////////////////////////////
  126.  
  127.  
  128. BOOL CTestWindow::OnCommand(WPARAM wParam, LPARAM lParam)
  129. {
  130.     if (wParam < IDS_COLOR_NAME_FIRST || wParam >= IDS_COLOR_NAME_FIRST + nColors)
  131.         return CFrameWnd::OnCommand(wParam, lParam);        // default
  132.  
  133.     // special color selected
  134.     CString strYouPicked;
  135.     strYouPicked.LoadString(IDS_YOU_PICKED_COLOR);
  136.  
  137.     CString strColor;
  138.     strColor.LoadString(wParam);
  139.  
  140.     CString strMsg;
  141.     strMsg.Format(strYouPicked, (LPCTSTR)strColor);
  142.  
  143.     CString strMenuTest;
  144.     strMenuTest.LoadString(IDS_MENU_TEST);
  145.  
  146.     MessageBox(strMsg, strMenuTest);
  147.  
  148.     return TRUE;
  149. }
  150.  
  151. /////////////////////////////////////////////////////////////////////////////
  152.