Exemplo n.º 1
0
        private int RegisterMenuItem(uint id,
                                     uint idCmdFirst,
                                     string text,
                                     bool enabled,
                                     IntPtr bitmap,
                                     IntPtr subMenu,
                                     uint position,
                                     IntPtr registerTo)
        {
            var sub = new MENUITEMINFO();

            sub.cbSize = (uint)Marshal.SizeOf(sub);

            var m = MIIM.MIIM_STRING | MIIM.MIIM_FTYPE | MIIM.MIIM_ID | MIIM.MIIM_STATE;

            if (bitmap != IntPtr.Zero)
            {
                m |= MIIM.MIIM_BITMAP;
            }
            if (subMenu != IntPtr.Zero)
            {
                m |= MIIM.MIIM_SUBMENU;
            }
            sub.fMask = m;

            sub.wID        = idCmdFirst + id;
            sub.fType      = MFT.MFT_STRING;
            sub.dwTypeData = text;
            sub.hSubMenu   = subMenu;
            sub.fState     = enabled ? MFS.MFS_ENABLED : MFS.MFS_DISABLED;
            sub.hbmpItem   = bitmap;

            if (!NativeMethods.InsertMenuItem(registerTo, position, true, ref sub))
            {
                return(Marshal.GetHRForLastWin32Error());
            }
            return(0);
        }
Exemplo n.º 2
0
 public static extern bool InsertMenuItem(
     IntPtr hMenu,
     uint uItem,
     [MarshalAs(UnmanagedType.Bool)] bool fByPosition,
     ref MENUITEMINFO mii);
Exemplo n.º 3
0
        /// <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));
            }

            // 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, 0, true, ref sep))
            {
                return(Marshal.GetHRForLastWin32Error());
            }

            // Register item 0: Submenu
            var hSubMenu = NativeMethods.CreatePopupMenu();
            var item     = menuItems[0];

            RegisterMenuItem(0, idCmdFirst, item.Text, true, item.Bitmap, hSubMenu, 1, hMenu);

            // Register item 1: RunDefault
            item = menuItems[1];
            RegisterMenuItem(1, idCmdFirst, item.Text, true, item.Bitmap, IntPtr.Zero, 0, hSubMenu);

            // Add a separator.
            sep        = new MENUITEMINFO();
            sep.cbSize = (uint)Marshal.SizeOf(sep);
            sep.fMask  = MIIM.MIIM_TYPE;
            sep.fType  = MFT.MFT_SEPARATOR;
            NativeMethods.InsertMenuItem(hSubMenu, 1, true, ref sep);

            // Register item 2 (Submenu->ManageApp).
            item = menuItems[2];
            RegisterMenuItem(2, idCmdFirst, item.Text, true, item.Bitmap, IntPtr.Zero, 2, hSubMenu);

            // Register item 3 (Submenu->ManageAll).
            item = menuItems[3];
            RegisterMenuItem(3, idCmdFirst, item.Text, true, item.Bitmap, IntPtr.Zero, 3, hSubMenu);

            //Register user-defined profiles.
            //We should count down to 4.
            for (var i = menuItems.Count - 1; i > 3; i--)
            {
                item = menuItems[i];
                if (item.ShowInMainMenu == true)
                {
                    RegisterMenuItem((uint)i, idCmdFirst, item.Text, item.Enabled, item.Bitmap, IntPtr.Zero, 1, hMenu);
                }
                else
                {
                    RegisterMenuItem((uint)i, idCmdFirst, item.Text, item.Enabled, item.Bitmap, IntPtr.Zero, 0, hSubMenu);
                }
            }

            // Add a separator.
            sep        = new MENUITEMINFO();
            sep.cbSize = (uint)Marshal.SizeOf(sep);
            sep.fMask  = MIIM.MIIM_TYPE;
            sep.fType  = MFT.MFT_SEPARATOR;
            NativeMethods.InsertMenuItem(hSubMenu,
                                         (uint)menuItems.FindAll(t => t.ShowInMainMenu != true).Count - 4,
                                         true,
                                         ref sep);

            // Return an HRESULT value with the severity set to SEVERITY_SUCCESS.
            // Set the code value to the total number of items added.
            return(WinError.MAKE_HRESULT(WinError.SEVERITY_SUCCESS, 0, 3 + (uint)menuItems.Count));
        }