private void UpdateMainMenuBrush() { // screen states where there is no menu yet if (Menu == null) return; if (WindowState == FormWindowState.Minimized) return; // alias colorized resources ColorizedResources cres = ColorizedResources.Instance; // dispose any existing brush and/or bitmaps if (_hMainMenuBrush != IntPtr.Zero) DisposeGDIObject(ref _hMainMenuBrush); if (_hMainMenuBitmap != IntPtr.Zero) DisposeGDIObject(ref _hMainMenuBitmap); if (_hMainMenuBrushBitmap != IntPtr.Zero) DisposeGDIObject(ref _hMainMenuBrushBitmap); if (_mainMenuBitmap != null) { Bitmap tmp = _mainMenuBitmap; _mainMenuBitmap = null; tmp.Dispose(); } // create a brush which contains the menu background _mainMenuBitmap = new Bitmap(Width, -RelativeWindowBounds.Y); _hMainMenuBitmap = _mainMenuBitmap.GetHbitmap(); using (Graphics g = Graphics.FromImage(_mainMenuBitmap)) { Rectangle bounds = MenuBounds; Debug.WriteLine("MenuBounds: " + bounds); if (cres.CustomMainMenuPainting) { // paint custom menu background CustomPaintMenuBackground(g, bounds); } else { using (Brush brush = new SolidBrush(SystemMainMenuColor)) g.FillRectangle(brush, bounds); } } _hMainMenuBrushBitmap = _mainMenuBitmap.GetHbitmap(); _hMainMenuBrush = Gdi32.CreatePatternBrush(_hMainMenuBrushBitmap); // set the brush MENUINFO mi = new MENUINFO(); mi.cbSize = Marshal.SizeOf(typeof(MENUINFO)); mi.fMask = MIM.BACKGROUND; mi.hbrBack = _hMainMenuBrush; User32.SetMenuInfo(Menu.Handle, ref mi); User32.DrawMenuBar(Handle); }
/// <summary> /// Add commands to a shortcut menu. /// </summary> /// <param name="hMenu">A handle to the shortcut menu.</param> /// <param name="iMenu"> /// The zero-based position at which to insert the first new menu item. /// </param> /// <param name="idCmdFirst"> /// The minimum value that the handler can specify for a menu item ID. /// </param> /// <param name="idCmdLast"> /// The maximum value that the handler can specify for a menu item ID. /// </param> /// <param name="uFlags"> /// Optional flags that specify how the shortcut menu can be changed. /// </param> /// <returns> /// If successful, returns an HRESULT value that has its severity value set /// to SEVERITY_SUCCESS and its code value set to the offset of the largest /// command identifier that was assigned, plus one. /// </returns> public int QueryContextMenu( IntPtr hMenu, uint iMenu, uint idCmdFirst, uint idCmdLast, uint uFlags) { // If uFlags include CMF_DEFAULTONLY then we should not do anything. if (((uint)CMF.CMF_DEFAULTONLY & uFlags) != 0) { return(WinError.MAKE_HRESULT(WinError.SEVERITY_SUCCESS, 0, 0)); } MENUINFO mnfo = new MENUINFO(); mnfo.cbSize = (UInt32)Marshal.SizeOf(mnfo); mnfo.fMask = MIM.MIM_STYLE; mnfo.dwStyle = MNS.MNS_CHECKORBMP; NativeMethods.SetMenuInfo(hMenu, ref mnfo); // Use either InsertMenu or InsertMenuItem to add menu items. MENUITEMINFO mii = new MENUITEMINFO(); mii.cbSize = (uint)Marshal.SizeOf(mii); mii.fMask = MIIM.MIIM_BITMAP | MIIM.MIIM_STRING | MIIM.MIIM_FTYPE | MIIM.MIIM_ID | MIIM.MIIM_STATE; mii.wID = idCmdFirst + IDM_DISPLAY; mii.fType = MFT.MFT_STRING; mii.dwTypeData = this.menuText; mii.fState = MFS.MFS_ENABLED; mii.hbmpItem = this.menuBmp; if (!NativeMethods.InsertMenuItem(hMenu, iMenu, true, ref mii)) { return(Marshal.GetHRForLastWin32Error()); } // Add a separator. MENUITEMINFO sep = new MENUITEMINFO(); sep.cbSize = (uint)Marshal.SizeOf(sep); sep.fMask = MIIM.MIIM_TYPE; sep.fType = MFT.MFT_SEPARATOR; if (!NativeMethods.InsertMenuItem(hMenu, iMenu + 1, true, ref sep)) { return(Marshal.GetHRForLastWin32Error()); } // Return an HRESULT value with the severity set to SEVERITY_SUCCESS. // Set the code value to the offset of the largest command identifier // that was assigned, plus one (1). return(WinError.MAKE_HRESULT(WinError.SEVERITY_SUCCESS, 0, IDM_DISPLAY + 1)); }
void TagManagedMenuItems(Menu menu, int tag) { MENUINFO info = new MENUINFO(); info.cbSize = (UInt32)Marshal.SizeOf(info); info.fMask = MIM.MIM_MENUDATA; info.dwMenuData = (UIntPtr)tag; foreach (MenuItem item in menu.MenuItems) { User32.SetMenuInfo(item.Handle, ref info); } }
private void TagManagedMenuItems(Menu menu, int tag) { var info = new MENUINFO(); info.cbSize = (uint)Marshal.SizeOf(info); info.fMask = MenuInfoMember.MIM_MENUDATA; info.dwMenuData = (UIntPtr)tag; foreach (MenuItem item in menu.MenuItems) { SetMenuInfo(item.Handle, info); } }
private void RemoveShellMenuItems(Menu menu) { const int tag = 0xAB; var menuInfo = new MENUINFO(); menuInfo.cbSize = (uint)Marshal.SizeOf(menuInfo); menuInfo.fMask = MenuInfoMember.MIM_MENUDATA; var itemInfo = new MENUITEMINFO(); itemInfo.cbSize = (uint)Marshal.SizeOf(itemInfo); itemInfo.fMask = MenuItemInfoMask.MIIM_ID | MenuItemInfoMask.MIIM_SUBMENU; // First, tag the managed menu items with an arbitary value (0xAB). TagManagedMenuItems(menu, tag); var remove = new List <uint>(); var count = GetMenuItemCount(menu.Handle); for (uint n = 0; n < count; ++n) { GetMenuItemInfo(menu.Handle, n, true, ref itemInfo); if (itemInfo.hSubMenu.IsNull) { // If the item has no submenu we can't get the tag, so check its ID to determine if it was added by the shell. if (itemInfo.wID >= m_CmdFirst) { remove.Add(n); } } else { GetMenuInfo(itemInfo.hSubMenu, ref menuInfo); if ((int)menuInfo.dwMenuData != tag) { remove.Add(n); } } } // Remove the unmanaged menu items. remove.Reverse(); foreach (var position in remove) { DeleteMenu(menu.Handle, (uint)position, MenuFlags.MF_BYPOSITION); } }
public void RemoveShellMenuItems(Menu menu) { const int tag = 0xAB; List <int> remove = new List <int>(); uint count = User32.GetMenuItemCount(menu.Handle); MENUINFO menuInfo = new MENUINFO(); MENUITEMINFO itemInfo = new MENUITEMINFO(); menuInfo.cbSize = Marshal.SizeOf(menuInfo); menuInfo.fMask = MIM.MIM_MENUDATA; itemInfo.cbSize = Marshal.SizeOf(itemInfo); itemInfo.fMask = MIIM.MIIM_ID | MIIM.MIIM_SUBMENU; // First, tag the managed menu items with an arbitary // value (0xAB). TagManagedMenuItems(menu, tag); for (int n = 0; n < count; ++n) { User32.GetMenuItemInfo(menu.Handle, n, true, ref itemInfo); if (itemInfo.hSubMenu == IntPtr.Zero) { // If the item has no submenu we can't get the tag, so // check its ID to determine if it was added by the shell. if (itemInfo.wID >= m_CmdFirst) { remove.Add(n); } } else { User32.GetMenuInfo(itemInfo.hSubMenu, ref menuInfo); if (menuInfo.dwMenuData != tag) { remove.Add(n); } } } // Remove the unmanaged menu items. remove.Reverse(); foreach (int position in remove) { User32.DeleteMenu(menu.Handle, position, MF.MF_BYPOSITION); } }
public static extern int SetMenuInfo(IntPtr hMenu, ref MENUINFO mi);
public static extern bool SetMenuInfo( MenuHandle hmenu, [In] ref MENUINFO lpcmi);
public static extern bool GetMenuInfo( MenuHandle hMenu, ref MENUINFO lpcmi);