/// <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. // Then only add the menu item if CMF_EXTENDEDVERBS (aka. shift+right-click) is set. if (((uint)CMF.CMF_DEFAULTONLY & uFlags) != 0 || ((uint)CMF.CMF_EXTENDEDVERBS & uFlags) == 0) { return(WinError.MAKE_HRESULT(WinError.SEVERITY_SUCCESS, 0, 0)); } // Use either InsertMenu or InsertMenuItem to add menu items. var 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 = MenuText; mii.fState = MFS.MFS_ENABLED; mii.hbmpItem = IntPtr.Zero; if (!NativeMethods.InsertMenuItem(hMenu, iMenu, true, ref mii)) { return(Marshal.GetHRForLastWin32Error()); } // Add a separator. /* * var 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)); }
public static extern bool InsertMenuItem( IntPtr hMenu, uint uItem, [MarshalAs(UnmanagedType.Bool)] bool fByPosition, ref MENUITEMINFO mii);