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

  1. // custmenu.cpp : custom menu
  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 "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: BASE + RGB bits : 8 colors max
  86.  
  87. #define IDM_COLOR_FIRST             0x7000
  88. #define IDM_COLOR_BLACK             (IDM_COLOR_FIRST + 0)
  89. #define IDM_COLOR_BLUE              (IDM_COLOR_FIRST + 1)
  90. #define IDM_COLOR_GREEN             (IDM_COLOR_FIRST + 2)
  91. #define IDM_COLOR_CYAN              (IDM_COLOR_FIRST + 3)
  92. #define IDM_COLOR_RED               (IDM_COLOR_FIRST + 4)
  93. #define IDM_COLOR_MAGENTA           (IDM_COLOR_FIRST + 5)
  94. #define IDM_COLOR_YELLOW            (IDM_COLOR_FIRST + 6)
  95. #define IDM_COLOR_WHITE             (IDM_COLOR_FIRST + 7)
  96.  
  97. #define IDM_COLOR_LAST              (IDM_COLOR_FIRST + 7)
  98.  
  99. /////////////////////////////////////////////////////////////////////////////
  100.  
  101. // Call AttachCustomMenu once
  102. //   it will replace the menu item with the ID  'IDM_TEST_CUSTOM_MENU'
  103. //   with a color menu popup
  104. // Replace the specified menu item with a color popup
  105. void CTestWindow::AttachCustomMenu()
  106. {
  107.     // now add a few new menu items
  108.     for (int iColor = 0; iColor <= (IDM_COLOR_LAST-IDM_COLOR_FIRST); iColor++)
  109.     {
  110.         // 3 bit encoded RGB values
  111.         BYTE red = (BYTE)(((iColor & 4) != 0) * 255);
  112.         BYTE green = (BYTE)(((iColor & 2) != 0) * 255);
  113.         BYTE blue = (BYTE)((iColor & 1) * 255);
  114.  
  115.         m_colorMenu.AppendColorMenuItem(IDM_COLOR_FIRST + iColor,
  116.             RGB(red, green, blue));
  117.     }
  118.  
  119.     // Replace the specified menu item with a color popup
  120.     //  (note: will only work once)
  121.     CMenu* pMenuBar = GetMenu();
  122.     ASSERT(pMenuBar != NULL);
  123.     TCHAR szString[256];     // don't change the string
  124.  
  125.     pMenuBar->GetMenuString(IDM_TEST_CUSTOM_MENU, szString, sizeof(szString),
  126.         MF_BYCOMMAND);
  127.     VERIFY(GetMenu()->ModifyMenu(IDM_TEST_CUSTOM_MENU, MF_BYCOMMAND | MF_POPUP,
  128.         (UINT)m_colorMenu.m_hMenu, szString));
  129. }
  130.  
  131. /////////////////////////////////////////////////////////////////////////////
  132.  
  133.  
  134. BOOL CTestWindow::OnCommand(WPARAM wParam, LPARAM lParam)
  135. {
  136.     if (wParam < IDM_COLOR_FIRST || wParam > IDM_COLOR_LAST)
  137.         return CFrameWnd::OnCommand(wParam, lParam);        // default
  138.  
  139.     // special color selected
  140.     CString strYouPicked;
  141.     strYouPicked.LoadString(IDS_YOU_PICKED_COLOR);
  142.  
  143.     CString strColor;
  144.     strColor.LoadString(IDS_COLOR_NAME_FIRST + wParam - IDM_COLOR_FIRST);
  145.  
  146.     CString strMsg;
  147.     strMsg.Format(strYouPicked, (LPCTSTR)strColor);
  148.  
  149.     CString strMenuTest;
  150.     strMenuTest.LoadString(IDS_MENU_TEST);
  151.  
  152.     MessageBox(strMsg, strMenuTest);
  153.  
  154.     return TRUE;
  155. }
  156.  
  157. /////////////////////////////////////////////////////////////////////////////
  158.