Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
4.2 KiB

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