/// <summary> /// Creates the native menu item. /// </summary> /// <param name="toolStripItem">The tool strip item.</param> /// <param name="id">The id.</param> /// <returns>The native menu ite,.</returns> private static MENUITEMINFO CreateNativeMenuItem(ToolStripItem toolStripItem, uint id) { // Create a menu item info, set its size. var menuItemInfo = new MENUITEMINFO(); menuItemInfo.cbSize = (uint)Marshal.SizeOf(menuItemInfo); menuItemInfo.wID = id; // Depending on the type of the item, we'll call the appropriate building function. if(toolStripItem is ToolStripMenuItem) BuildMenuItemInfo(ref menuItemInfo, (ToolStripMenuItem)toolStripItem); else if(toolStripItem is ToolStripSeparator) BuildSeparatorMenuItemInfo(ref menuItemInfo); // Return the menu item info. return menuItemInfo; }
/// <summary> /// Builds the menu item info. /// </summary> /// <param name="menuItemInfo">The menu item info.</param> /// <param name="menuItem">The menu item.</param> private static void BuildMenuItemInfo(ref MENUITEMINFO menuItemInfo, ToolStripMenuItem menuItem) { // Set the mask - we're interested in essentially everything. menuItemInfo.fMask = (uint)(MIIM.MIIM_BITMAP | MIIM.MIIM_STRING | MIIM.MIIM_FTYPE | MIIM.MIIM_ID | MIIM.MIIM_STATE); // If the menu item has children, we'll also create the submenu. if (menuItem.HasDropDownItems) { menuItemInfo.fMask += (uint) MIIM.MIIM_SUBMENU; menuItemInfo.hSubMenu = User32.CreatePopupMenu(); } // The type is the string. menuItemInfo.fType = (uint)MFT.MFT_STRING; // The type data is the text of the menu item. menuItemInfo.dwTypeData = menuItem.Text; // The state is enabled. menuItemInfo.fState = menuItem.Enabled ? (uint)MFS.MFS_ENABLED : (uint)(MFS.MFS_DISABLED | MFS.MFS_GRAYED); // If the menu item is checked, add the check state. if (menuItem.Checked) { menuItemInfo.fState += (uint) MFS.MFS_CHECKED; } // Is there an icon? menuItemInfo.hbmpItem = menuItem.Image != null ? new Bitmap(menuItem.Image).GetHbitmap() : IntPtr.Zero; }
/// <summary> /// Builds the menu item info. /// </summary> /// <param name="menuItemInfo">The menu item info.</param> private static void BuildSeparatorMenuItemInfo(ref MENUITEMINFO menuItemInfo) { // Set the mask to the type only, and the type to the separator type. menuItemInfo.fMask = (uint)MIIM.MIIM_TYPE; menuItemInfo.fType = (uint)MFT.MFT_SEPARATOR; }
internal static extern bool InsertMenuItem(IntPtr hMenu, uint uItem, bool fByPosition, [In] ref MENUITEMINFO lpmii);