menuHelpers.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include "./menuHelpers.h"
  2. #include <strsafe.h>
  3. INT MenuHelper_CopyMenuEx(HMENU hDest, INT iDstStart, HMENU hSource, INT iSrcStart, INT iSrcCount)
  4. {
  5. if (!hDest || !hSource) return 0;
  6. TCHAR szText[1024] = {0};
  7. MENUITEMINFO mii = {sizeof(MENUITEMINFO), };
  8. mii.fMask = MIIM_BITMAP | MIIM_CHECKMARKS | MIIM_DATA | MIIM_FTYPE | MIIM_ID | MIIM_STATE | MIIM_STRING | MIIM_SUBMENU;
  9. mii.dwTypeData = szText;
  10. if (iDstStart < 0) iDstStart = 0;
  11. if (iSrcStart < 0) iSrcStart = 0;
  12. INT pos = iDstStart;
  13. if ( 0 != iSrcCount)
  14. {
  15. INT c = GetMenuItemCount(hSource);
  16. if (-1 == c || iSrcStart > c) return 0;
  17. if (iSrcCount < 0)
  18. iSrcCount = c - iSrcStart;
  19. else if (iSrcCount < (c - iSrcStart))
  20. c = iSrcCount + iSrcStart;
  21. for (int i = iSrcStart; i < c; i++)
  22. {
  23. mii.cch = ARRAYSIZE(szText);
  24. if (GetMenuItemInfo(hSource, i, TRUE, &mii))
  25. {
  26. if(InsertMenuItem(hDest, pos, TRUE, &mii))
  27. {
  28. pos++;
  29. }
  30. }
  31. }
  32. }
  33. else
  34. {
  35. mii.cch = ARRAYSIZE(szText);
  36. if (GetMenuItemInfo(hSource, iSrcStart, FALSE, &mii))
  37. {
  38. if (InsertMenuItem(hDest, pos, TRUE, &mii))
  39. {
  40. pos++;
  41. }
  42. }
  43. }
  44. return pos - iDstStart;
  45. }
  46. INT MenuHelper_CopyMenu(HMENU hDest, INT iDstStart, HMENU hSource)
  47. {
  48. return MenuHelper_CopyMenuEx(hDest, iDstStart, hSource, 0, -1);
  49. }
  50. HMENU MenuHelper_DuplcateMenu(HMENU hMenu)
  51. {
  52. HMENU hDest = CreatePopupMenu();
  53. if (NULL == hMenu)
  54. return NULL;
  55. MenuHelper_CopyMenu(hDest, 0, hMenu);
  56. return hDest;
  57. }
  58. INT MenuHelper_InsertSeparator(HMENU hMenu, INT iPos)
  59. {
  60. if (!hMenu) return FALSE;
  61. MENUITEMINFO mii = {sizeof(MENUITEMINFO), };
  62. mii.fMask = MIIM_FTYPE;
  63. mii.fType = MFT_SEPARATOR;
  64. return InsertMenuItem(hMenu, iPos, TRUE, &mii);
  65. }
  66. void MenuHelper_RemoveRange(HMENU hMenu, UINT min, UINT max)
  67. {
  68. MENUITEMINFO mii = {sizeof(MENUITEMINFO), };
  69. mii.fMask = MIIM_ID;
  70. INT count = GetMenuItemCount(hMenu);
  71. if (-1 == count)
  72. return;
  73. while(count--)
  74. {
  75. if (GetMenuItemInfo(hMenu, count, TRUE, &mii) &&
  76. mii.wID >= min && mii.wID <= max)
  77. {
  78. RemoveMenu(hMenu, count, MF_BYPOSITION);
  79. }
  80. }
  81. }
  82. void MenuHelper_EnableGroup(HMENU hMenu, UINT *pszItems, INT cchItems, BOOL fByPos, BOOL bEnable)
  83. {
  84. UINT enableGroup = (bEnable) ? MF_ENABLED : (MF_DISABLED | MF_GRAYED);
  85. if (!fByPos)
  86. enableGroup |= MF_BYCOMMAND;
  87. for (INT i = 0; i < cchItems; i++)
  88. {
  89. EnableMenuItem(hMenu, pszItems[i], enableGroup);
  90. }
  91. }
  92. BOOL MenuHelper_GetMenuItemPos(HMENU hMenu, UINT itemId, INT *pPos)
  93. {
  94. if (pPos)
  95. *pPos = -1;
  96. MENUITEMINFO mii = {sizeof(MENUITEMINFO), };
  97. mii.fMask = MIIM_ID;
  98. INT count = GetMenuItemCount(hMenu);
  99. if (count < 1)
  100. return FALSE;
  101. while(count--)
  102. {
  103. if (GetMenuItemInfo(hMenu, count, TRUE, &mii) &&
  104. mii.wID == itemId)
  105. {
  106. if (pPos)
  107. *pPos = count;
  108. return TRUE;
  109. }
  110. }
  111. return FALSE;
  112. }