private static void ChangeMenuItem(string oldLabel, string newLabel, bool enabled) { try { IntPtr hMenu = Win32.SendMessage(nppData._nppHandle, NppMsg.NPPM_GETMENUHANDLE, (int)NppMsg.NPPPLUGINMENU, 0); //Try to find the Snip2Code menu MenuItemInfoEnlarged s2cMenu = ManageMenu(hMenu, PluginName, "", true, true); if (s2cMenu != null) { //in this Menu, find the item that matches the old label and change it: ManageMenu(s2cMenu.mif.hSubMenu, oldLabel, newLabel, enabled, false); } } catch (Exception e) { if (log != null) { log.ErrorFormat("Cannot change menu item {0} to {1} due to {2}", oldLabel.PrintNull(), newLabel.PrintNull(), e.Message); } } }
private static MenuItemInfoEnlarged ManageMenu(IntPtr parentMenu, string oldMenuName, string newMenuName, bool enable, bool onlyRead) { if ((parentMenu.ToInt32() == 0) || string.IsNullOrEmpty(oldMenuName)) { if (log != null) { log.WarnFormat("Cannot manage menu with parent={0} and oldMenuName={1}", parentMenu.ToInt32(), oldMenuName.PrintNull()); } return(null); } for (int i = Win32.GetMenuItemCount(parentMenu) - 1; i >= 0; i--) { try { //guess the label of the menu in two steps: //1. get the length of the string: MENUITEMINFO mif = new MENUITEMINFO(); mif.fMask = Win32.MIIM_STRING | Win32.MIIM_SUBMENU | Win32.MIIM_ID; mif.fType = Win32.MFT_STRING; mif.dwTypeData = IntPtr.Zero; bool res = Win32.GetMenuItemInfo(parentMenu, i, true, mif); if (!res) { uint error = Win32.GetLastError(); //just for debug purpose if (log != null) { log.WarnFormat("Cannot get menu info with parent={0} and # {1} due to error={2}", parentMenu.ToInt32(), i, error); } continue; } mif.cch++; mif.dwTypeData = Marshal.AllocHGlobal((IntPtr)(mif.cch * 2)); //2. get the actual string: try { res = Win32.GetMenuItemInfo(parentMenu, i, true, mif); if (!res) { uint error = Win32.GetLastError(); //just for debug purpose if (log != null) { log.WarnFormat("Cannot get menu info with parent={0} and # {1} due to error={2}", parentMenu.ToInt32(), i, error); } continue; } string caption = Marshal.PtrToStringUni(mif.dwTypeData); if (caption.ToLowerInvariant().StartsWith(oldMenuName.ToLowerInvariant())) { if (!onlyRead && !string.IsNullOrEmpty(newMenuName)) { mif.fMask = Win32.MIIM_STRING | Win32.MIIM_STATE; if (enable) { mif.fState = Win32.MFS_ENABLED; } else { mif.fState = Win32.MFS_DISABLED; } mif.dwTypeData = Marshal.StringToHGlobalUni(newMenuName); res = Win32.SetMenuItemInfo(parentMenu, i, true, mif); if (res) { if (!Win32.DrawMenuBar(nppData._nppHandle)) { uint error = Win32.GetLastError(); //just for debug purpose if (log != null) { log.WarnFormat("Cannot draw menu info with parent={0} and # {1} due to error={2}; newManuName was:{3}", parentMenu.ToInt32(), i, error, newMenuName.PrintNull()); } continue; } } else { uint error = Win32.GetLastError(); //just for debug purpose if (log != null) { log.WarnFormat("Cannot get menu info with parent={0} and # {1} due to error={2}; newManuName was:{3}", parentMenu.ToInt32(), i, error, newMenuName.PrintNull()); } continue; } } MenuItemInfoEnlarged result = new MenuItemInfoEnlarged(); result.mif = mif; result.pos = i; return(result); } } finally { Marshal.FreeHGlobal(mif.dwTypeData); } } catch { } } return(null); }