/// <summary> /// Refreshes the list of items, when the system menu has been modified by other functions that are not in this class. CAUTION! ALL ITEMS WILL LOSE THEIR CLICK EVENT! /// </summary> public void ReloadItems() { Items.Clear(); for (int i = 0; i < SysMenuAPI.GetMenuItemCount(Menu); i++) { Items.Add(new SystemMenuItem(this, Menu, SysMenuAPI.GetMenuItemID(Menu, i))); } }
/// <summary> /// Create a new object to work with the form's system menu. To check the user input, put <code>CallEvents</code> in WndProc of the form. /// </summary> /// <param name="form">he form from which the menu is to be obtained</param> public SystemMenu(Form form) { Items = new List <SystemMenuItem>(); OwnerForm = form; Menu = SysMenuAPI.GetSystemMenu(form.Handle); ReloadItems(); }
/// <summary> /// Removes all elements (including menu items added by default) from the menu. /// </summary> public void ClearMenu() { for (int i = Items.Count - 1; i >= 0; i--) { SysMenuAPI.RemoveMenu(Menu, Items[i].ID, SysMenuAPI.Flags.MF_BYCOMMAND); Items.RemoveAt(i); } }
/// <summary> /// Remove an item from the system menu. /// </summary> /// <param name="index">The index of the item which should be removed.</param> public void RemoveItem(int index) { SysMenuAPI.RemoveMenu(Menu, Items[index].ID, SysMenuAPI.Flags.MF_BYCOMMAND); if (Items[index].HasSubItems()) { Items[index].ClearSubMenu(); } Items.RemoveAt(index); }
/// <summary> /// Remove an item from the sub menu. /// </summary> /// <param name="index">The index of the item which should be removed.</param> public void RemoveSubItem(int index) { SystemMenuItem itm = SubItems[index]; SysMenuAPI.RemoveMenu(itm.HMenu, itm.ID, SysMenuAPI.Flags.MF_BYCOMMAND); if (itm.HasSubItems()) { itm.ClearSubMenu(); } SubItems.RemoveAt(index); if (SubItems.Count == 0) { SubMenu = IntPtr.Zero; } }
/// <summary> /// Add an item at a certain index to the system menu. /// </summary> /// <param name="type">The type of the item.</param> /// <param name="index">The index the item should be added.</param> /// <param name="text">The text of the item.</param> /// <returns>The item itself.</returns> public SystemMenuItem InsertItem(SystemMenuItemType type, int index, string text = "") { List <int> usedIDs = GetUsedIDs(); int id = 0; while (usedIDs.Contains(id)) { id++; } SystemMenuItem item = new SystemMenuItem(this, Menu, id); SysMenuAPI.InsertMenu(Menu, index, SysMenuAPI.Flags.MF_BYPOSITION | (type == SystemMenuItemType.STRING ? SysMenuAPI.MenuTypes.MF_STRING : SysMenuAPI.MenuTypes.MF_SEPARATOR), id, text); Items.Insert(index, item); return(item); }
/// <summary> /// Add an item to the system menu. /// </summary> /// <param name="type">The type of the item.</param> /// <param name="text">The text of the item.</param> /// <returns>The item itself.</returns> public SystemMenuItem AddItem(SystemMenuItemType type, string text = "") { List <int> usedIDs = GetUsedIDs(); int id = 0; while (usedIDs.Contains(id)) { id++; } SystemMenuItem item = new SystemMenuItem(this, Menu, id); SysMenuAPI.AppendMenu(Menu, type == SystemMenuItemType.STRING ? SysMenuAPI.MenuTypes.MF_STRING : SysMenuAPI.MenuTypes.MF_SEPARATOR, id, text); Items.Add(item); return(item); }
/// <summary> /// Removes all sub items. /// </summary> public void ClearSubMenu() { for (int i = SubItems.Count - 1; i >= 0; i--) { if (SubItems[i].HasSubItems()) { SubItems[i].ClearSubMenu(); } SysMenuAPI.RemoveMenu(SubItems[i].HMenu, SubItems[i].ID, SysMenuAPI.Flags.MF_BYCOMMAND); SubItems.RemoveAt(i); } if (SubItems.Count == 0) { SubMenu = IntPtr.Zero; } }
/// <summary> /// Set an system icon as default bitmap icon. /// </summary> /// <param name="icon">The icon type.</param> public void SetSystemIcon(SysMenuAPI.MenuItemInfo_hItem icon) { MenuItemInfo mInfo = new MenuItemInfo() { cbSize = Marshal.SizeOf(typeof(MenuItemInfo)), fMask = SysMenuAPI.FMask.MIIM_STRING | SysMenuAPI.FMask.MIIM_BITMAP | SysMenuAPI.FMask.MIIM_FTYPE | SysMenuAPI.FMask.MIIM_STATE | SysMenuAPI.FMask.MIIM_ID, fType = SysMenuAPI.FType.MFT_STRING, fState = SysMenuAPI.FState.MFS_ENABLED, wID = ID, hbmpItem = (IntPtr)icon, hbmpChecked = IntPtr.Zero, hbmpUnchecked = IntPtr.Zero, dwTypeData = Marshal.StringToHGlobalAuto(Text), dwItemData = IntPtr.Zero, hSubMenu = IntPtr.Zero, cch = Text.Length, }; SysMenuAPI.SetMenuItemInfo(HMenu, ID, false, ref mInfo); }
/// <summary> /// Add a item at a certain position to a sub menu of this item. This method will create a submenu if there is no one present. /// </summary> /// <param name="index">The index in the menu list the item should be added.</param> /// <param name="type">The type of the item.</param> /// <param name="text">The text of the item.</param> /// <returns>The item itself.</returns> public SystemMenuItem InsertSubItem(int index, SystemMenuItemType type, string text = "") { List <int> usedIDs = SysMenu.GetUsedIDs(); int id = 0; while (usedIDs.Contains(id)) { id++; } IntPtr p = SubMenu; if (p == IntPtr.Zero) { p = SysMenuAPI.CreateMenu(); SubMenu = p; } SystemMenuItem itm = new SystemMenuItem(SysMenu, p, id); SubItems.Insert(index, itm); SysMenuAPI.InsertMenu(p, index, SysMenuAPI.Flags.MF_BYPOSITION | (type == SystemMenuItemType.STRING ? SysMenuAPI.MenuTypes.MF_STRING : SysMenuAPI.MenuTypes.MF_SEPARATOR), id, text); return(itm); }
/// <summary> /// Add a item to a sub menu of this item. This method will create a submenu if there is no one present. /// </summary> /// <param name="type">The type of the item.</param> /// <param name="text">The text of the item.</param> /// <returns>The item itself.</returns> public SystemMenuItem AddSubItem(SystemMenuItemType type, string text = "") { List <int> usedIDs = SysMenu.GetUsedIDs(); int id = 0; while (usedIDs.Contains(id)) { id++; } IntPtr p = SubMenu; if (p == IntPtr.Zero) { p = SysMenuAPI.CreateMenu(); SubMenu = p; } SystemMenuItem itm = new SystemMenuItem(SysMenu, p, id); SubItems.Add(itm); SysMenuAPI.AppendMenu(p, type == SystemMenuItemType.STRING ? SysMenuAPI.MenuTypes.MF_STRING : SysMenuAPI.MenuTypes.MF_SEPARATOR, id, text); return(itm); }