Example #1
0
 public bool InsertSeparator(uint position)
 {
     MENUITEMINFO it = new MENUITEMINFO();
     it.cbSize = (uint)Marshal.SizeOf(it);
     it.fMask = MIIM_FTYPE;
     it.fType = MFT_SEPARATOR;
     return InsertMenuItem(_hSysMenu, position, true, ref it);
 }
Example #2
0
        internal static MENUITEMINFO CreateSeparator()
        {
            MENUITEMINFO sep = new MENUITEMINFO();
            sep.cbSize = (uint)Marshal.SizeOf(sep);
            sep.fMask = MIIM.TYPE;
            sep.fType = MFT.SEPARATOR;

            return sep;
        }
Example #3
0
 public bool InsertMenuItem(uint menuId,string name,uint position)
 {
     MENUITEMINFO it = new MENUITEMINFO();
     it.cbSize = (uint)Marshal.SizeOf(it);
     it.fMask = MIIM_STRING | MIIM_ID;
     it.wID = menuId;
     it.dwTypeData = name;
     return InsertMenuItem(_hSysMenu, position, true, ref it);
 }
Example #4
0
 internal static MENUITEMINFO CreateMenu(String text, uint id)
 {
     MENUITEMINFO mii = new MENUITEMINFO();
     mii.cbSize = (uint)Marshal.SizeOf(mii);
     mii.fMask = MIIM.ID | MIIM.STRING | MIIM.SUBMENU;
     mii.wID = id;
     mii.fType = MFT.STRING;
     mii.dwTypeData = text;
     return mii;
 }
Example #5
0
 internal static void AddStateMenuItem(IntPtr hMenu, string text, uint position)
 {
     MENUITEMINFO mii = new MENUITEMINFO();
     mii.cbSize = (uint)Marshal.SizeOf(mii);
     mii.fMask = MIIM.ID | MIIM.TYPE | MIIM.STATE;
     mii.fType = MFT.STRING;
     mii.dwTypeData = text;
     mii.fState = MFS.DISABLED;
     Import.InsertMenuItem(hMenu, position, true, ref mii);
 }
Example #6
0
        public void RefillChildrenList()
        {
            this.Children = new MenuTree();

            int Items = NativeMethods.GetMenuItemCount(this);

            for (int i = 0; i < Items; i++)
            {
                MENUITEMINFO ii = new MENUITEMINFO
                {
                    cbSize     = Marshal.SizeOf(typeof(MENUITEMINFO)),
                    fMask      = MenuItemInfofMask.MIIM_ID | MenuItemInfofMask.MIIM_SUBMENU | MenuItemInfofMask.MIIM_STRING | MenuItemInfofMask.MIIM_DATA | MenuItemInfofMask.MIIM_FTYPE | MenuItemInfofMask.MIIM_BITMAP | MenuItemInfofMask.MIIM_CHECKMARKS,
                    cch        = 50,
                    dwTypeData = Marshal.AllocHGlobal(50),
                    dwItemData = Marshal.AllocHGlobal(50),
                    //hbmpChecked = Marshal.AllocHGlobal(150),
                    //hbmpUnchecked = Marshal.AllocHGlobal(150),
                    //hbmpItem = Marshal.AllocHGlobal(150)
                };

                if (NativeMethods.GetMenuItemInfo(this, (uint)i, true, ref ii))
                {
                    if (ii.fType == MenuItemInfofType.MFT_OWNERDRAW)
                    {
                        ii.hbmpItem = Marshal.AllocHGlobal(1024);
                    }

                    string tt = ii.TypeData;

                    if (!string.IsNullOrEmpty(tt))
                    {
                        if (ii.hSubMenu != IntPtr.Zero)
                        {
                            this.Children[tt] = new SubMenu(this, tt, ii, Owner);
                        }
                        else
                        {
                            this.Children[tt] = new MenuItem(this, tt, ii);
                        }
                    }
                    else
                    {
                        tt = i.ToString();
                        this.Children[tt] = new MenuItem(this, tt, ii);
                    }
                    this.Children[tt].Position = i;
                }
            }
        }
Example #7
0
        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);
            }
        }
Example #8
0
        public static void AddSysMenuSubItem(string text, uint Position, uint ID, IntPtr hMenu)
        {
            MENUITEMINFO mii = new MENUITEMINFO();

            mii.fMask      = MenuInfoFlags.MIIM_ID | MenuInfoFlags.MIIM_TYPE | MenuInfoFlags.MIIM_STATE;
            mii.wID        = ID;
            mii.hSubMenu   = hMenu;
            mii.fType      = MenuItemFlags.MF_STRING;
            mii.dwTypeData = text;
            mii.cch        = (uint)mii.dwTypeData.Length;
            mii.fState     = MenuItemFlags.MF_ENABLED;
            mii.cbSize     = MENUITEMINFO.sizeOf;

            InsertMenu(hMenu, Position, mii);
        }
Example #9
0
        public MenuItem GetItem(uint position)
        {
            MENUITEMINFO nativeItem = new MENUITEMINFO();

            nativeItem.cbSize = Convert.ToUInt32(Marshal.SizeOf <MENUITEMINFO>());
            nativeItem.fMask  = MenuConstants.MIIM_BITMAP | MenuConstants.MIIM_CHECKMARKS | MenuConstants.MIIM_FTYPE | MenuConstants.MIIM_ID | MenuConstants.MIIM_STATE | MenuConstants.MIIM_SUBMENU;

            bool success = NativeMethods.GetMenuItemInfoW(Handle, position, true, ref nativeItem);

            if (!success)
            {
                throw new System.ComponentModel.Win32Exception();
            }
            return(new MenuItem(nativeItem));
        }
Example #10
0
        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);
            }
        }
Example #11
0
        private uint GetTextLength(IntPtr hMenu, int i, bool byPosition)
        {
            var info = new MENUITEMINFO();

            info.cbSize = Convert.ToUInt32(Marshal.SizeOf(info));
            info.fMask  = MIIM.MIIM_TYPE;
            if (!User32.GetMenuItemInfo(
                    hMenu,
                    i,
                    byPosition,
                    ref info))
            {
                throw new Win32Exception();
            }
            return(info.cch);
        }
Example #12
0
 internal ShellMenuItem x625a8691de7c9283(IntPtr x66504ce4f96688f4)
 {
     foreach (ShellMenuItem item in this.xa73e17ae29927ab0)
     {
         MENUITEMINFO structure = new MENUITEMINFO();
         structure.x2e94540690ec6f24 = (uint)Marshal.SizeOf(structure);
         structure.x8240369a843c7611 = 4;
         int num = item.xc8519eab1f8249ff(item.xeaf1b27180c0557b);
         User32.GetMenuItemInfo(item._owner.x4b1e528311f74227, (uint)num, 1, ref structure);
         if (structure.x66504ce4f96688f4 == x66504ce4f96688f4)
         {
             return(item);
         }
     }
     return(null);
 }
        public NativeMenuItem Insert(int insertAfter, string text, Bitmap bmp, bool fbyPos, IntPtr data)
        {
            var            mii = new MENUITEMINFO();
            MemPtr         mm  = new MemPtr();
            NativeMenuItem nmi = null;

            // If insertAfter = -1 Then insertAfter = 0

            mii.cbSize = Marshal.SizeOf(mii);

            // if the text is nothing or '-' we'll assume they want it to be a separator
            if (text is null || text == "-")
            {
                mii.dwTypeData = IntPtr.Zero;
                mii.fType      = (int)User32.MFT_MENUBREAK;
            }
Example #14
0
        public void DestroySubMenu()
        {
            var mii = new MENUITEMINFO();

            mii.cbSize = Marshal.SizeOf(mii);
            mii.fMask  = User32.MIIM_SUBMENU;
            User32.GetMenuItemInfo(hMenu, itemId, false, ref mii);
            if (mii.hSubMenu != IntPtr.Zero)
            {
                User32.DestroyMenu(mii.hSubMenu);
            }

            User32.DestroyMenu(mii.hSubMenu);
            mii.hSubMenu = IntPtr.Zero;
            User32.SetMenuItemInfo(hMenu, itemId, false, ref mii);
        }
Example #15
0
        public void AddSystemMenuItem(IntPtr applicationHandle, ApplicationType application)
        {
            /// Get the Handle for the System Menu of the provided handle
            var systemMenuHandle = WindowsApi.GetSystemMenu(applicationHandle, WindowsApi.B_REVERT);
            var count            = WindowsApi.GetMenuItemCount(systemMenuHandle);

            var separator = new MENUITEMINFO
            {
                cbSize = (uint)Marshal.SizeOf(typeof(MENUITEMINFO)),
                fMask  = WindowsApi.MIIM_TYPE | WindowsApi.MIIM_ID,
                fState = 0,
                fType  = WindowsApi.MF_SEPARATOR,
                wID    = application == ApplicationType.LocalApplication
                                        ? WindowsApi.LOCAL_MENU_ITEM_SEPARATOR
                                        : WindowsApi.CALCULATOR_MENU_SEPARATOR
            };

            var id = application == ApplicationType.LocalApplication
                                        ? WindowsApi.LOCAL_MENU_ITEM_ID
                                        : WindowsApi.CALCULATOR_MENU_ITEM_ID;
            var menuItem = new MENUITEMINFO
            {
                cbSize     = (uint)Marshal.SizeOf(typeof(MENUITEMINFO)),
                cch        = 255,
                dwTypeData = WindowsApi.MENU_ITEM_TEXT,
                fMask      = WindowsApi.MIIM_STATE | WindowsApi.MIIM_ID | WindowsApi.MIIM_TYPE,
                fState     = application == ApplicationType.LocalApplication
                                        ? WindowsApi.MFS_ENABLED
                                        : WindowsApi.MFS_DISABLED,
                fType = WindowsApi.MF_STRING,
                wID   = id
            };

            // Add a separator and a new menu item before the last menu item, Close
            WindowsApi.InsertMenuItem(systemMenuHandle, (uint)(count - 1), true /* fByPosition */, ref separator);
            WindowsApi.InsertMenuItem(systemMenuHandle, (uint)(count - 1), true /* fByPosition */, ref menuItem);

            // Add an icon to th menu item for the LocalApplication.
            if (application == ApplicationType.LocalApplication)
            {
                var icon = WindowsApi.LoadImage((IntPtr)null, $"..\\..\\Resources\\MenuItemIcon.bmp", WindowsApi.IMAGE_BITMAP, 0, 0, WindowsApi.LR_LOADFROMFILE);
                WindowsApi.SetMenuItemBitmaps(systemMenuHandle, id, WindowsApi.MF_BYCOMMAND, icon, icon);
            }

            // This function must be called to draw the changed menu bar because the menu bar changed after the system had created the window.
            WindowsApi.DrawMenuBar(systemMenuHandle);
        }
        /// <summary>
        /// Builds a native context menu, on to the provided HMENU.
        /// </summary>
        /// <param name="hMenu">The handle to the menu.</param>
        /// <param name="firstItemId">The first item id.</param>
        /// <param name="toolStripItems">The tool strip menu items.</param>
        /// <returns>The index of the last item created.</returns>
        public uint BuildNativeContextMenu(IntPtr hMenu, uint firstItemId, ToolStripItemCollection toolStripItems)
        {
            //  Create an ID counter and position counter.
            var  idCounter       = firstItemId;
            uint positionCounter = 0;

            //  Go through every tool strip item.
            foreach (ToolStripItem item in toolStripItems)
            {
                //  If there's not a name, set one.
                //  This will be used as the verb.
                if (string.IsNullOrEmpty(item.Name))
                {
                    item.Name = Guid.NewGuid().ToString();
                }

                //  Map the command by verb and id.
                verbsToCommands[item.Name] = item;
                idsToItems[idCounter]      = item;

                //  Create the native menu item info.
                MENUITEMINFO menuItemInfo = CreateNativeMenuItem(item, idCounter);

                //  Insert the native menu item.
                if (User32.InsertMenuItem(hMenu, positionCounter, true, ref menuItemInfo) == false)
                {
                    //  We failed to build the item, so don't try and do anything more with it.
                    continue;
                }

                //  We successfully created the menu item, so increment the counters.
                indexedCommands.Add(item);
                idCounter++;
                positionCounter++;

                //  Have we just built a menu item? If so, does it have child items?
                var toolStripMenuItem = item as ToolStripMenuItem;
                if (toolStripMenuItem != null && toolStripMenuItem.HasDropDownItems)
                {
                    //  Create each drop down item.
                    idCounter = BuildNativeContextMenu(menuItemInfo.hSubMenu, idCounter, toolStripMenuItem.DropDownItems);
                }
            }

            //  Return the counter.
            return(idCounter);
        }
Example #17
0
        internal int xc8519eab1f8249ff(int xeaf1b27180c0557b)
        {
            int menuItemCount = x443cc432acaadb1d.GetMenuItemCount(_owner.x4b1e528311f74227);

            for (uint i = 0; i < menuItemCount; i++)
            {
                MENUITEMINFO structure = new MENUITEMINFO();
                structure.x2e94540690ec6f24 = (uint)Marshal.SizeOf(structure);
                structure.x8240369a843c7611 = 2;
                User32.GetMenuItemInfo(_owner.x4b1e528311f74227, i, 1, ref structure);
                if (structure.x9f8d45ce61065766 == xeaf1b27180c0557b)
                {
                    return((int)i);
                }
            }
            return(-1);
        }
    /// <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));
        }

        // 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 = menuText;
        mii.fState     = MFS.MFS_ENABLED;
        mii.hbmpItem   = 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));
    }
Example #19
0
        private List <MenuInfo> GenerateMenuInfo(IntPtr menu, IContextMenu contextMenu, IContextMenu3 contextMenu3)
        {
            if (menu == IntPtr.Zero)
            {
                return(new List <MenuInfo>());
            }
            var count = User32.GetMenuItemCount(menu);

            return(Enumerable.Range(0, count).Select(it =>
            {
                var cch = GetTextLength(menu, it, true) + 1;
                var itemInfo = new MENUITEMINFO();
                itemInfo.cbSize = (uint)Marshal.SizeOf(itemInfo);
                itemInfo.fMask = MIIM.MIIM_STRING | MIIM.MIIM_SUBMENU | MIIM.MIIM_ID;
                itemInfo.cch = cch;
                itemInfo.dwTypeData = Marshal.AllocCoTaskMem((int)cch * sizeof(char));
                itemInfo.hSubMenu = IntPtr.Zero;
                User32.GetMenuItemInfo(menu, it, true, ref itemInfo);
                var title = Marshal.PtrToStringAuto(itemInfo.dwTypeData);
                Marshal.FreeCoTaskMem(itemInfo.dwTypeData);
                var id = Convert.ToInt64(itemInfo.wID);
                var command = string.Empty;
                if (!string.IsNullOrEmpty(title) && id > 0 && uint.TryParse(id + "", out var uid))
                {
                    command = GetCommandString(contextMenu, uid, true);
                }
                if (itemInfo.hSubMenu != IntPtr.Zero)
                {
                    Marshal.AllocCoTaskMem(sizeof(int)).UseCoTask(indexPointer =>
                    {
                        Marshal.WriteInt32(indexPointer, it);
                        contextMenu3.HandleMenuMsg2(KAN.WM_INITMENUPOPUP, itemInfo.hSubMenu, indexPointer,
                                                    out _);
                    });
                }
                return new MenuInfo
                {
                    CommandName = command,
                    Title = title,
                    Id = id,
                    SubMenu = GenerateMenuInfo(itemInfo.hSubMenu, contextMenu, contextMenu3)
                };
            }).ToList());
        }
Example #20
0
        /// <summary>
        /// 菜单句柄无效会抛异常,项不存在返回null
        /// </summary>
        static MENUITEMINFO GetMenuItemInfo(IntPtr hMenu, int item, bool byPosition)
        {
            var info = new MENUITEMINFO
            {
                fMask = 0x2     //MIIM_ID
                        | 0x1   //MIIM_STATE
                        | 0x4   //MIIM_SUBMENU
                        | 0x40  //MIIM_STRING
                        | 0x100 //MIIM_FTYPE,不要用MIIM_TYPE
            };

            try
            {
                //第一次执行得到cch(菜单文本字节数)
                RetrieveInfo(hMenu, item, byPosition, info);

                info.cch++;                                   //将字节数+1,以容纳\0
                info.dwTypeData = new string('\0', info.cch); //并开辟好接收空间

                //再次执行得到准确的菜单文本
                RetrieveInfo(hMenu, item, byPosition, info);

                return(info);
            }
            catch (Win32Exception ex) when(ex.NativeErrorCode == ERROR_MENU_ITEM_NOT_FOUND)
            {
                return(null);
            }

            void RetrieveInfo(IntPtr hMenu_, int item_, bool byPosition_, MENUITEMINFO info_)
            {
                if (!NativeGetMenuItemInfo(hMenu_, (uint)item_, byPosition_, info_))
                {
                    //该API能正确反馈项不存在,但不能反馈句柄无效
                    var code = Marshal.GetLastWin32Error();
                    if (code != ERROR_MENU_ITEM_NOT_FOUND)
                    {
                        code = ERROR_INVALID_MENU_HANDLE;
                    }

                    throw new Win32Exception(code);
                }
            }
        }
        internal override void AddToMenu(IntPtr menu, uint itemPosition, ref uint itemId, Dictionary <uint, DeskBandMenuAction> callbacks)
        {
            _menuiteminfo = new MENUITEMINFO()
            {
                cbSize     = Marshal.SizeOf <MENUITEMINFO>(),
                fMask      = MENUITEMINFO.MIIM.MIIM_TYPE | MENUITEMINFO.MIIM.MIIM_STATE | MENUITEMINFO.MIIM.MIIM_ID,
                fType      = MENUITEMINFO.MFT.MFT_STRING,
                dwTypeData = Text,
                cch        = (uint)Text.Length,
                wID        = itemId++,
            };

            _menuiteminfo.fState |= Enabled ? MENUITEMINFO.MFS.MFS_ENABLED : MENUITEMINFO.MFS.MFS_DISABLED;
            _menuiteminfo.fState |= Checked ? MENUITEMINFO.MFS.MFS_CHECKED : MENUITEMINFO.MFS.MFS_UNCHECKED;

            callbacks[_menuiteminfo.wID] = this;

            User32.InsertMenuItem(menu, itemPosition, true, ref _menuiteminfo);
        }
Example #22
0
 public static void DrawMenuItemBitmaps(this Menu Menu)
 {
     foreach (MenuItem mi in Menu.MenuItems)
     {
         if (!MenuItemIcons.ContainsKey(mi))
         {
             continue;
         }
         Image    image  = MenuItemIcons[mi];
         Bitmap   bitmap = new Bitmap(image.Width, image.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
         Graphics g      = Graphics.FromImage(bitmap);
         g.DrawImage(image, 0, 0, image.Width, image.Height);
         MENUITEMINFO mii = new MENUITEMINFO();
         mii.cbSize   = (uint)Marshal.SizeOf(typeof(MENUITEMINFO));
         mii.fMask    = 0x80u;
         mii.hbmpItem = bitmap.GetHbitmap(Color.FromArgb(0, 0, 0, 0));
         SetMenuItemInfo(mi.Parent.Handle, (uint)mi.Index, true, ref mii);
     }
 }
Example #23
0
 public MenuItem(string MenuText, MenuFlags Flags, int ItemId = 0, MenuItem_Handler Handler = null)
 {
     this.MenuText = MenuText;
     this.Flags    = Flags;
     if (ItemId == 0)
     {
         ItemId = ("SFS" + MenuText).GetHashCode();
     }
     this.ItemInfo = new MENUITEMINFO
     {
         wID        = ItemId,
         dwTypeData = Marshal.StringToHGlobalAnsi(MenuText),
         dwItemData = new IntPtr(ItemId),
         cch        = MenuText.Length + 1,
         fState     = MenuItemInfofState.MFS_ENABLED,
         fType      = MenuItemInfofType.MFT_STRING
     };
     this.Handler = Handler;
 }
Example #24
0
        private static object GetMenuItemInfo(IntPtr hMenu, uint uItem, bool fByPosition, uint fMask)
        {
            object obj = null;

            MENUITEMINFO mii = new MENUITEMINFO();

            mii.cbSize     = (uint)System.Runtime.InteropServices.Marshal.SizeOf(mii);
            mii.fMask      = fMask;
            mii.dwTypeData = null;

            if (GetMenuItemInfo(hMenu, uItem, fByPosition, ref mii))
            {
                if (fMask == MIIM_STRING)
                {
                    string pszText = new string('\0', (int)mii.cch + 1);
                    mii.dwTypeData = pszText;
                    mii.cch        = mii.cch + 1;
                    if (GetMenuItemInfo(hMenu, uItem, fByPosition, ref mii))
                    {
                        obj = mii.dwTypeData;
                    }
                }
                else if (fMask == MIIM_DATA)
                {
                    obj = mii.dwItemData;
                }
                else if (fMask == MIIM_STATE)
                {
                    obj = mii.fState;
                }
                else if (fMask == MIIM_FTYPE)
                {
                    obj = mii.fType;
                }
                else if (fMask == MIIM_BITMAP)
                {
                    obj = mii.hbmpItem.ToInt32();
                }
            }

            return(obj);
        }
        public static string GetMenuItemText(IntPtr hMenu, int itemId, bool byPos = true)
        {
            var mii = new MENUITEMINFO();
            var mm  = new SafePtr();

            mii.cbSize = Marshal.SizeOf(mii);
            mii.cch    = 0;
            mii.fMask  = User32.MIIM_TYPE;

            User32.GetMenuItemInfo(hMenu, itemId, byPos, ref mii);

            mm.Length = (mii.cch + 1) * sizeof(char);

            mii.cch       += 1;
            mii.dwTypeData = mm.handle;

            if (User32.GetMenuItemInfo(hMenu, itemId, byPos, ref mii) == 0)
            {
                int err = User32.GetLastError();

                mm.Length = 1026L;
                mm.ZeroMemory();

                User32.FormatMessage(0x1000U, IntPtr.Zero, (uint)err, 0U, mm.handle, 512U, IntPtr.Zero);

                mm.Dispose();

                return(null);
            }

            if ((mii.fType & User32.MFT_SEPARATOR) == User32.MFT_SEPARATOR)
            {
                return("-");
            }
            else
            {
                string s;
                s = mm.ToString();
                mm.Dispose();
                return(s);
            }
        }
Example #26
0
        public void RemoveItem(int index)
        {
            int itemCount = ItemCount;

            if ((index >= 0) && (index < itemCount))
            {
                MENUITEMINFO structure = new MENUITEMINFO();
                structure.x2e94540690ec6f24 = (uint)Marshal.SizeOf(structure);
                structure.x8240369a843c7611 = 2;
                User32.GetMenuItemInfo(x4b1e528311f74227, (uint)index, 1, ref structure);
                IntPtr subMenu = x443cc432acaadb1d.GetSubMenu(x4b1e528311f74227, index);
                if (subMenu != IntPtr.Zero)
                {
                    new ShellMenu(subMenu, this, 0).RemoveAll();
                    x443cc432acaadb1d.DestroyMenu(subMenu);
                }
                x443cc432acaadb1d.RemoveMenu(x4b1e528311f74227, index, 0x400);
                x1dbcf1591cc2cceb.x2237804176f4caea(this, (int)structure.x9f8d45ce61065766);
            }
        }
Example #27
0
            internal MenuItemInfo(HMENU hMenu, uint idx)
            {
                using var strmem = new SafeHGlobalHandle(512);
                var mii = new MENUITEMINFO
                {
                    cbSize     = (uint)Marshal.SizeOf(typeof(MENUITEMINFO)),
                    fMask      = MenuItemInfoMask.MIIM_ID | MenuItemInfoMask.MIIM_SUBMENU | MenuItemInfoMask.MIIM_FTYPE | MenuItemInfoMask.MIIM_STRING | MenuItemInfoMask.MIIM_STATE | MenuItemInfoMask.MIIM_BITMAP,
                    fType      = MenuItemType.MFT_STRING,
                    dwTypeData = (IntPtr)strmem,
                    cch        = strmem.Size / (uint)StringHelper.GetCharSize()
                };

                Win32Error.ThrowLastErrorIfFalse(GetMenuItemInfo(hMenu, idx, true, ref mii));
                Id           = mii.wID;
                Text         = mii.fType.IsFlagSet(MenuItemType.MFT_SEPARATOR) ? "-" : mii.fType.IsFlagSet(MenuItemType.MFT_STRING) ? strmem.ToString(-1, CharSet.Auto) : "";
                Type         = mii.fType;
                State        = mii.fState;
                BitmapHandle = mii.hbmpItem;
                SubMenus     = GetMenuItems(mii.hSubMenu);
            }
Example #28
0
        public static List <MENUITEMINFO> GetMenuInfo(HandleRef hWnd)
        {
            var lst   = new List <MENUITEMINFO>();
            var count = GetMenuItemCount(hWnd);

            for (var i = 0; i < count; i++)
            {
                var mif = new MENUITEMINFO(MIIM.STRING | MIIM.ID | MIIM.BITMAP | MIIM.DATA | MIIM.STATE);
                var res = GetMenuItemInfo(hWnd, (uint)i, true, mif);
                if (res)
                {
                    ++mif.cch;
                    mif.dwTypeData = new string(' ', (int)mif.cch);
                    _ = GetMenuItemInfo(hWnd, (uint)i, true, mif);
                }

                lst.Add(mif);
            }

            return(lst);
        }
        /// <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);
        }
Example #30
0
        public static void AddSysMenuItem(string text, uint ID, uint Position, IntPtr hMenu)
        {
            MENUITEMINFO mii       = new MENUITEMINFO();
            IntPtr       m_SysMenu = IntPtr.Zero;

            // Check system menu handle
            if (m_SysMenu == IntPtr.Zero)
            {
                m_SysMenu = apiGetSystemMenu(Handle, false);
            }

            // Create new menu item info
            if (hMenu != IntPtr.Zero)
            {
                mii.fMask    = MenuInfoFlags.MIIM_ID | MenuInfoFlags.MIIM_TYPE | MenuInfoFlags.MIIM_STATE | MenuInfoFlags.MIIM_SUBMENU;
                mii.hSubMenu = hMenu;
            }
            else
            {
                mii.fMask = MenuInfoFlags.MIIM_ID | MenuInfoFlags.MIIM_TYPE | MenuInfoFlags.MIIM_STATE;
            }

            mii.wID = ID;
            if (text == "-")
            {
                mii.fType = MenuItemFlags.MF_SEPARATOR;
            }
            else
            {
                mii.fType = MenuItemFlags.MF_STRING;
            }

            mii.dwTypeData = text;
            mii.cch        = (uint)mii.dwTypeData.Length;
            mii.fState     = MenuItemFlags.MF_ENABLED;
            mii.cbSize     = MENUITEMINFO.sizeOf;

            // Insert it
            InsertMenu(m_SysMenu, Position, mii);
        }
Example #31
0
        private static void DumpMenu(IntPtr Handler)
        {
            int MenuCount = GetMenuItemCount(Handler);

            if (MenuCount == -1)
            {
                return;
            }
            var MenuInfo = new MENUITEMINFO();

            for (int i = 0; i < MenuCount; i++)
            {
                MenuInfo = new MENUITEMINFO()
                {
                    cbSize     = MENUITEMINFO.SizeOf,
                    fMask      = MIIM_STRING | MIIM_SUBMENU,
                    fType      = MFT_STRING,
                    dwTypeData = new string(new char[1024]),
                    cch        = 1025
                };

                bool Sucess = GetMenuItemInfo(Handler, i, true, ref MenuInfo);

                string Text = MenuInfo.dwTypeData;

                if (MenuInfo.hSubMenu != IntPtr.Zero)
                {
                    DumpMenu(MenuInfo.hSubMenu);
                }

                if (Texts.Contains(Text) || string.IsNullOrWhiteSpace(Text))
                {
                    continue;
                }

                Texts.Add(Text);

                Console.WriteLine("Text Hooked: {0}", Text);
            }
        }
        public Form1()
        {
            InitializeComponent();
            IntPtr hMenu = GetSystemMenu(Handle, false);

            if (hMenu != IntPtr.Zero)
            {
                var menuInfo = new MENUITEMINFO
                {
                    cbSize     = (uint)Marshal.SizeOf(typeof(MENUITEMINFO)),
                    cch        = 255,
                    dwTypeData = "Test Item",
                    fMask      = 0x1 | 0x2 | 0x10,
                    fState     = 0,
                    // Add an ID for your Menu Item
                    wID   = 0x1,
                    fType = 0x0
                };
                InsertMenuItem(hMenu, 0, true, ref menuInfo);
                DrawMenuBar(Handle);
            }
        }
Example #33
0
        public void CheckForCalculatorAndAddMenuItem()
        {
            var handle = WindowsApi.FindWindow(null, "Calculator");

            if (handle != IntPtr.Zero)
            {
                var systemMenuHandle = WindowsApi.GetSystemMenu(handle, WindowsApi.B_REVERT);

                var lpmii = new MENUITEMINFO
                {
                    cbSize     = (uint)Marshal.SizeOf(typeof(MENUITEMINFO)),
                    dwTypeData = null,
                    fMask      = WindowsApi.MIIM_STATE | WindowsApi.MIIM_ID | WindowsApi.MIIM_TYPE,
                    fType      = WindowsApi.MF_STRING
                };

                if (systemMenuHandle != IntPtr.Zero && !WindowsApi.GetMenuItemInfo(systemMenuHandle, WindowsApi.CALCULATOR_MENU_ITEM_ID, WindowsApi.BY_IDENTIFIER, ref lpmii))
                {
                    AddSystemMenuItem(handle, ApplicationType.Calculator);
                }
            }
        }
Example #34
0
        public void UpdateMenu(IWindowMenuItem menuItem)
        {
            var mii = new MENUITEMINFO(null)
            {
                fMask = MenuItemInfoMember.MIIM_CHECKMARKS |
                        MenuItemInfoMember.MIIM_DATA |
                        MenuItemInfoMember.MIIM_FTYPE |
                        MenuItemInfoMember.MIIM_ID |
                        MenuItemInfoMember.MIIM_STATE |
                        MenuItemInfoMember.MIIM_STRING
            };

            PInvokeUtils.Try(() => SystemMenuAPI.GetMenuItemInfo(_hSysMenu, menuItem.Id, false, ref mii));

            if (menuItem.Enabled)
            {
                mii.fState &= (~MenuItemState.MFS_DISABLED); // clear "disabled" flag
            }
            else
            {
                mii.fState |= MenuItemState.MFS_DISABLED;    // set "disabled" flag
            }
            if (menuItem.Checked)
            {
                mii.fState |= MenuItemState.MFS_CHECKED;     // set "checked" flag
            }
            else
            {
                mii.fState &= (~MenuItemState.MFS_CHECKED);  // clear "checked" flag
            }
            mii.fMask = MenuItemInfoMember.MIIM_STATE;

            PInvokeUtils.Try(() => SystemMenuAPI.SetMenuItemInfo(_hSysMenu, menuItem.Id, false, ref mii));

            // TODO: From my observations, this function always returns false, even though it appears to succeed.
            //       Am I using it incorrectly?
            SystemMenuAPI.DrawMenuBar(_hSysMenu);
        }
Example #35
0
        int IContextMenu.QueryContextMenu(uint hMenu, uint iMenu, int idCmdFirst, int idCmdLast, uint uFlags)
        {
            int id = 1;
            if ((uFlags & 0xf) == 0 || (uFlags & (uint)CMF.CMF_EXPLORE) != 0)
            {
                uint nselected = Helpers.DragQueryFile(m_hDrop, 0xffffffff, null, 0);
                if (nselected == 1)
                {
                    uint hmnuPopup = Helpers.CreatePopupMenu();
                    id = PopulateMenu(hmnuPopup, idCmdFirst + id);

                    MENUITEMINFO mii = new MENUITEMINFO();
                    mii.cbSize = 48;
                    mii.fMask = (uint)MIIM.TYPE | (uint)MIIM.STATE | (uint)MIIM.SUBMENU;
                    mii.hSubMenu = (int)hmnuPopup;
                    mii.fType = (uint)MF.STRING;
                    mii.dwTypeData = "ACTIONS";
                    mii.fState = (uint)MF.ENABLED;
                    Helpers.InsertMenuItem(hMenu, (uint)iMenu, 1, ref mii);
                }
            }
            return id;
        }
Example #36
0
        public uint InsertSeperatorPrivate(IntPtr subMenu, int position)
        {
            MENUITEMINFO mii = new MENUITEMINFO();

            mii.cbSize = ( uint )Marshal.SizeOf(typeof(MENUITEMINFO));
            mii.fMask  = ( uint )MIIM.ID;
            mii.wID    = ( uint )(mMenuCmdFirst + mMenuItemsAddedCount);
            mii.fType  = ( uint )MF.SEPARATOR;
            mii.cch    = 0;
            mii.fState = ( uint )MF.ENABLED;

            // Add it to the item
            if (DllImports.InsertMenuItem(subMenu, (uint)position, true, ref mii) == 0)
            {
                // failed
                return(0);
            }
            else
            {
                mMenuItemsAddedCount++;
                return(mii.wID);
            }
        }
Example #37
0
        int IContextMenu.QueryContextMenu(uint hMenu, uint iMenu, int idCmdFirst, int idCmdLast, uint uFlags)
        {
            int id = 1;
            if ( (uFlags & 0xf) == 0 || (uFlags & (uint)CMF.CMF_EXPLORE) != 0)
            {
                uint nselected = Helpers.DragQueryFile(m_hDrop, 0xffffffff, null, 0);
                if (nselected == 1)
                {
                    StringBuilder sb = new StringBuilder(1024);
                    Helpers.DragQueryFile(m_hDrop, 0, sb, sb.Capacity + 1);
                    string directory = sb.ToString();
                    uint attr = Helpers.GetFileAttributes(directory);
                    if ((attr & Helpers.FILE_ATTRIBUTE_DIRECTORY) != 0)
                    {
                        // I have selected a bona-fide directory add to the menus.
                        MENUITEMINFO mii = new MENUITEMINFO();
                        mii.cbSize		= 48;
                        mii.fMask		= (uint)MIIM.ID | (uint)MIIM.TYPE | (uint)MIIM.STATE;

                        foreach(MenuItem item in m_items)
                        {
                            mii.wID			= idCmdFirst + id;
                            mii.fType		= (uint)MF.STRING;
                            mii.dwTypeData	= item.Text;
                            mii.fState		= (uint)MF.ENABLED;
                            Helpers.InsertMenuItem(hMenu, (uint)2, 1, ref mii);
                            id++;
                        }
                        // Go and get the menus from the registry
                        if (m_items.Length > 0)
                        {
                            mii.fType		= (uint)MF.SEPARATOR;
                            mii.fState		= (uint)MF.ENABLED;
                            Helpers.InsertMenuItem(hMenu, (uint)2, 1, ref mii);
                        }
                    }
                }
            }
            return id;
        }
Example #38
0
        internal static extern int SetMenuItemInfo(uint hMenu, uint uItem, uint fByPosition, 
			ref MENUITEMINFO mii);
 private void GenerateMenuItem(ContextMenu view, String header, int id, bool isRadio = false, uint atPosition = 0) {
   MENUITEMINFO miidetails = new MENUITEMINFO();
   miidetails.cbSize = (uint)Marshal.SizeOf(miidetails);
   miidetails.fMask = MIIM.MIIM_STRING | MIIM.MIIM_ID | MIIM.MIIM_FTYPE | MIIM.MIIM_STATE;
   miidetails.fState = (uint)(isRadio ? 0x00000008 : 0x0);
   miidetails.fType = 0 | 0x00000200;
   miidetails.wID = id;
   miidetails.dwItemData = IntPtr.Zero;
   miidetails.dwTypeData = header;
   User32.InsertMenuItem(view.Handle, atPosition, true, ref miidetails);
 }
Example #40
0
        public static void AddMenuItem(uint hMenu, string text, int id, uint
			position, bool active, bool bold)
        {
            MENUITEMINFO mii = new MENUITEMINFO();
            mii.cbSize = 48;
            mii.wID = id;

            mii.fMask = (uint)MIIM.ID | (uint)MIIM.TYPE | (uint)MIIM.STATE;
            mii.fType = (uint)MF.STRING;

            mii.dwTypeData  = text;
            mii.fState = (uint)MF.ENABLED;
            if (!active)
                mii.fState = mii.fState | (uint)MF.DISABLED | (uint)MF.GRAYED;
            if (bold)
                mii.fState = mii.fState | (uint)MF.DEFAULT;
            MenuHelper.InsertMenuItem(hMenu, position, 1, ref mii);
        }
Example #41
0
        public void UpdateMenu(SystemMenuItem menuItem)
        {
            var mii = new MENUITEMINFO
                      {
                          fMask = MIIM_CHECKMARKS | MIIM_DATA | MIIM_FTYPE | MIIM_ID | MIIM_STATE | MIIM_STRING
                      };
            mii.cbSize = (uint) Marshal.SizeOf(mii);

            PInvokeUtils.Try(() => GetMenuItemInfo(_hSysMenu, menuItem.Id, false, ref mii));

            if (menuItem.Enabled)
                mii.fState &= (~MFS_DISABLED); // clear "disabled" flag
            else
                mii.fState |= MFS_DISABLED;    // set "disabled" flag

            if (menuItem.Checked)
                mii.fState |= MFS_CHECKED;    // set "checked" flag
            else
                mii.fState &= (~MFS_CHECKED); // clear "checked" flag

            mii.fMask = MIIM_STATE;

            PInvokeUtils.Try(() => SetMenuItemInfo(_hSysMenu, menuItem.Id, false, ref mii));

            // TODO: From my observations, this function always returns false, even though it appears to succeed.
            //       Am I using it incorrectly?
            DrawMenuBar(_hSysMenu);
        }
        private void initCommands()
        {
            commands = new StringCollection();
            IntPtr hMenu = Win32.SendMessage(Npp.NppHandle, NppMsg.NPPM_GETMENUHANDLE, 1, 0);
            MENUITEMINFO mif = new MENUITEMINFO();
            string caption1, caption2, caption3;
            int cmdId = 0;
            IntPtr hSubMenu, hSubSubMenu;
            for (int i = 0; i < GetMenuItemCount(hMenu); i++) {
                mif.fMask = 0x00000040;
                mif.fType = 0x00000000;
                mif.dwTypeData = IntPtr.Zero;
                bool res = GetMenuItemInfo(hMenu, i, true, mif);
                if (!res) continue;
                mif.cch++;
                mif.dwTypeData = Marshal.AllocHGlobal((IntPtr)(mif.cch*2));
                try {
                    res = GetMenuItemInfo(hMenu, i, true, mif);
                    if (!res) continue;
                    caption1 = Marshal.PtrToStringUni(mif.dwTypeData);
                    if (caption1.Equals("jN") || caption1.Equals("&?") || caption1.Equals("&Language"))
                        continue;
                }
                finally {
                    Marshal.FreeHGlobal(mif.dwTypeData);
                }

                mif.fMask = 0x00000004;
                res = GetMenuItemInfo(hMenu, i, true, mif);
                if (!res) continue;
                hSubMenu = mif.hSubMenu;

                //sub menu
                for (int j = 0; j < GetMenuItemCount(hSubMenu); j++) {
                    mif.fMask = 0x00000040;
                    mif.fType = 0x00000000;
                    mif.dwTypeData = IntPtr.Zero;
                    res = GetMenuItemInfo(hSubMenu, j, true, mif);
                    if (!res) continue;
                    mif.cch++;
                    mif.dwTypeData = Marshal.AllocHGlobal((IntPtr)(mif.cch*2));
                    try {
                        res = GetMenuItemInfo(hSubMenu, j, true, mif);
                        if (!res) continue;
                        caption2 = Marshal.PtrToStringUni(mif.dwTypeData);
                        caption2 = caption2.Replace("&", "");
                    }
                    finally {
                        Marshal.FreeHGlobal(mif.dwTypeData);
                    }
                    if (string.IsNullOrWhiteSpace(caption2)) continue;

                    mif.fMask = 0x00000004;
                    res = GetMenuItemInfo(hSubMenu, j, true, mif);
                    if (!res) continue;
                    hSubSubMenu = mif.hSubMenu;
                    if ((int)hSubSubMenu < 1) {
                        cmdId = GetMenuItemID(hSubMenu, j);
                        caption2 = Regex.Replace(caption2, "(Alt|Ctrl|Shift).*$", "");
                        caption2 = Regex.Replace(caption2, "F[0-9]{1,2}.*$", "");
                        commands.Add(caption1.Replace("&", "") + "::" + caption2 + "@Menu@" + cmdId);
                    }
                    else {
                        for (int k = 0; k < GetMenuItemCount(hSubSubMenu); k++) {
                            mif.fMask = 0x00000040;
                            mif.fType = 0x00000000;
                            mif.dwTypeData = IntPtr.Zero;
                            res = GetMenuItemInfo(hSubSubMenu, k, true, mif);
                            if (!res) continue;
                            mif.cch++;
                            mif.dwTypeData = Marshal.AllocHGlobal((IntPtr)(mif.cch*2));
                            try {
                                res = GetMenuItemInfo(hSubSubMenu, k, true, mif);
                                if (!res) continue;
                                caption3 = Marshal.PtrToStringUni(mif.dwTypeData);
                                if (caption3.IndexOf("About") > -1) continue;
                            }
                            finally {
                                Marshal.FreeHGlobal(mif.dwTypeData);
                            }
                            if (string.IsNullOrWhiteSpace(caption3)) continue;

                            mif.fMask = 0x00000004;
                            res = GetMenuItemInfo(hSubSubMenu, k, true, mif);
                            if (!res) continue;
                            cmdId = GetMenuItemID(hSubSubMenu, k);
                            caption3 = caption3.Replace("&", "");
                            caption3 = Regex.Replace(caption3, "(Alt|Ctrl|Shift).*$", "");
                            caption3 = Regex.Replace(caption3, "F[0-9]{1,2}.*$", "");
                            commands.Add(caption1.Replace("&", "") + "::" + caption2 + "::" + caption3 + "@Menu@" + cmdId);
                        }
                    }
                }
            }
        }
 private void GenerateMenuItemExecutable(ContextMenu view, String header, int id) {
   MENUITEMINFO miidetails = new MENUITEMINFO();
   miidetails.cbSize = (uint)Marshal.SizeOf(miidetails);
   miidetails.fMask = MIIM.MIIM_STRING | MIIM.MIIM_ID | MIIM.MIIM_FTYPE | MIIM.MIIM_STATE;
   miidetails.fState = 0x0;
   miidetails.fType = 0;
   miidetails.wID = id;
   miidetails.dwItemData = IntPtr.Zero;
   miidetails.dwTypeData = header;
   User32.InsertMenuItem(view.Handle, 0, true, ref miidetails);
 }
 private void GenerateSeparator(ContextMenu view) {
   MENUITEMINFO miidetails = new MENUITEMINFO();
   miidetails.cbSize = (uint)Marshal.SizeOf(miidetails);
   miidetails.fMask = MIIM.MIIM_FTYPE;
   miidetails.fType = 2048;
   User32.InsertMenuItem(view.Handle, 0, true, ref miidetails);
 }
		public uint InsertSeperatorPrivate( IntPtr subMenu, int position )
		{
			MENUITEMINFO mii = new MENUITEMINFO();
			mii.cbSize = ( uint )Marshal.SizeOf( typeof( MENUITEMINFO ) );
			mii.fMask = ( uint )MIIM.ID;
			mii.wID = ( uint )( mMenuCmdFirst + mMenuItemsAddedCount );
			mii.fType = ( uint )MF.SEPARATOR;
			mii.cch = 0;
			mii.fState = ( uint )MF.ENABLED;

			// Add it to the item
			if ( DllImports.InsertMenuItem( subMenu, (uint)position, true, ref mii ) == 0 )
			{
				// failed
				return 0;
			}
			else
			{
				mMenuItemsAddedCount++;
				return mii.wID;
			}
		}
Example #46
0
File: api.cs Project: zapu/Algenhax
 public static extern bool GetMenuItemInfo(IntPtr hMenu, uint uItem, bool fByPosition, ref MENUITEMINFO lpmii);
        int IContextMenu.QueryContextMenu(IntPtr hmenu, uint iMenu, int idCmdFirst, int idCmdLast, uint uFlags)
        {
            int id = (int)iMenu;

            if (!CheckExtension()) return id;

            MENUITEMINFO sep = new MENUITEMINFO();
            sep.cbSize = (uint)Marshal.SizeOf(sep);
            sep.fMask = (uint)MIIM.TYPE;
            sep.fType = (uint)MF.SEPARATOR;

            InsertMenuItem(hmenu, iMenu, 1, ref sep);

            id++;

            MENUITEMINFO mii = new MENUITEMINFO();
            mii.cbSize = (uint)Marshal.SizeOf(mii);
            mii.fMask = (uint)(MIIM.STRING | MIIM.FTYPE | MIIM.ID | MIIM.STATE);
            mii.wID = idCmdFirst + id;
            mii.fType = (uint)MF.STRING;
            mii.dwTypeData = "RBuild";
            mii.fState = (uint)MF.ENABLED;

            InsertMenuItem(hmenu, iMenu + 1, 1, ref mii);

            id++;

            sep = new MENUITEMINFO();
            sep.cbSize = (uint)Marshal.SizeOf(sep);
            sep.fMask = (uint)MIIM.TYPE;
            sep.fType = (uint)MF.SEPARATOR;

            InsertMenuItem(hmenu, iMenu + 2, 1, ref sep);

            id++;

            return id;
        }
    void RemoveShellMenuItems(Menu menu) {
      const int tag = 0xAB;
      var remove = new List<int>();
      int count = User32.GetMenuItemCount(menu.Handle);
      var menuInfo = new MENUINFO();
      var itemInfo = new MENUITEMINFO();

      menuInfo.cbSize = Marshal.SizeOf(menuInfo);
      menuInfo.fMask = MIM.MIM_MENUDATA;
      itemInfo.cbSize = (uint)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);
      }
    }
Example #49
0
 internal static extern bool InsertMenuItem(HMenu hmenu, uint uposition, uint uflags, ref MENUITEMINFO mii);
 private void GenerateSubmenu(ContextMenu child, ContextMenu parent, String header) {
   MENUITEMINFO miiview = new MENUITEMINFO();
   miiview.cbSize = (uint)Marshal.SizeOf(miiview);
   miiview.fMask = MIIM.MIIM_STRING | MIIM.MIIM_FTYPE | MIIM.MIIM_STATE | MIIM.MIIM_SUBMENU;
   miiview.fState = 0x0;
   miiview.fType = 0;
   miiview.hSubMenu = child.Handle;
   miiview.dwItemData = IntPtr.Zero;
   miiview.dwTypeData = header;
   User32.InsertMenuItem(parent.Handle, 0, true, ref miiview);
 }
Example #51
0
        int PopulateMenu(uint hMenu, int id)
        {
            AddMenuItem(hMenu, "SHA-1", id, 0);
            AddMenuItem(hMenu, "MD5", ++id, 1);
            AddMenuItem(hMenu, "SHA-256", ++id, 2);
            AddMenuItem(hMenu, "SHA-384", ++id, 3);
            AddMenuItem(hMenu, "SHA-512", ++id, 4);
            AddMenuItem(hMenu, "RIPEMD-160", ++id, 5);

            // Add a separator
            MENUITEMINFO sep = new MENUITEMINFO();
            sep.cbSize = 48;
            sep.fMask = (uint)MIIM.TYPE;
            sep.fType = (uint)MF.SEPARATOR;
            sep.wID = ++id;
            Helpers.InsertMenuItem(hMenu, (uint)6, 1, ref sep);

            //Add "Include File Date" date check indicator
            MENUITEMINFO mii = new MENUITEMINFO();
            mii.cbSize = 48;
            mii.fMask = (uint)MIIM.ID | (uint)MIIM.TYPE | (uint)MIIM.STATE;
            mii.wID = ++id;
            mii.fType = (uint)MF.STRING;
            mii.dwTypeData = "Include File Date";
            if (Properties.Settings.Default.IncludeDate)
                mii.fState = (uint)MF.ENABLED | (uint)MF.CHECKED;
            else
                mii.fState = (uint)MF.ENABLED | (uint)MF.UNCHECKED;
            Helpers.InsertMenuItem(hMenu, (uint)7, 1, ref mii);

            //Add "Include File Size" check indicator
            mii = new MENUITEMINFO();
            mii.cbSize = 48;
            mii.fMask = (uint)MIIM.ID | (uint)MIIM.TYPE | (uint)MIIM.STATE;
            mii.wID = ++id;
            mii.fType = (uint)MF.STRING;
            mii.dwTypeData = "Include File Size";
            if (Properties.Settings.Default.IncludeFileSize)
                mii.fState = (uint)MF.ENABLED | (uint)MF.CHECKED;
            else
                mii.fState = (uint)MF.ENABLED | (uint)MF.UNCHECKED;
            Helpers.InsertMenuItem(hMenu, (uint)8, 1, ref mii);

            return id++;
        }
Example #52
0
 public static extern bool InsertMenuItem(IntPtr hMenu, uint uItem, bool fByPosition, ref MENUITEMINFO info);
Example #53
0
        internal static extern int InsertMenuItem(uint hmenu, uint uposition,
			uint uflags, ref MENUITEMINFO mii);
Example #54
0
 internal static bool SaveMenuItem(IntPtr hMenu, uint iMenu, ref MENUITEMINFO mii)
 {
     return Import.InsertMenuItem(hMenu, iMenu, true, ref mii);
 }
		public uint InsertSubMenuIntoSubMenu( IntPtr menu, IntPtr subMenu, string textName, int position )
		{
			MENUITEMINFO mii = new MENUITEMINFO();
			mii.cbSize = (uint)Marshal.SizeOf( typeof( MENUITEMINFO ) );
			mii.fMask = (uint)MIIM.ID | (uint)MIIM.STRING | (uint)MIIM.SUBMENU;
			mii.wID = (uint)( mMenuCmdFirst + mMenuItemsAddedCount );
			mii.fType = (uint)MF.STRING;
			mii.dwTypeData = textName;
			mii.cch = 0;
			mii.fState = (uint)MF.ENABLED;
			mii.hSubMenu = subMenu;

			// Add it to the item
			if ( DllImports.InsertMenuItem( menu, (uint)position, true, ref mii ) == 0 )
			{
				// failed
				return 0;
			}
			else
			{
				mMenuItemsAddedCount++;
				return mii.wID;
			}
		}
Example #56
0
        // IContextMenu
        /// <summary>
        /// Add the "Calculate File Hash" menu item to the Context menu
        /// </summary>
        /// <param name="hMenu"></param>
        /// <param name="iMenu"></param>
        /// <param name="idCmdFirst"></param>
        /// <param name="idCmdLast"></param>
        /// <param name="uFlags"></param>
        /// <returns></returns>
        int IContextMenu.QueryContextMenu(uint hMenu, uint iMenu, int idCmdFirst, int idCmdLast, uint uFlags)
        {
            // Create the popup to insert
            uint hmnuPopup = Helpers.CreatePopupMenu();
            int id = 1;
            if ( (uFlags & 0xf) == 0 || (uFlags & (uint)CMF.CMF_EXPLORE) != 0)
            {
                uint nselected = Helpers.DragQueryFile(m_hDrop, 0xffffffff, null, 0);
                if (nselected > 0)
                {
                    for (uint i = 0; i < nselected; i++)
                    {
                        StringBuilder sb = new StringBuilder(1024);
                        Helpers.DragQueryFile(m_hDrop, i, sb, sb.Capacity + 1);
                        fileNames.Add(sb.ToString());
                    }
                    // Populate the popup menu with file-specific items
                    id = PopulateMenu(hmnuPopup, idCmdFirst + id);
                }
                else
                    return 0;

                // Add the popup to the context menu
                MENUITEMINFO mii = new MENUITEMINFO();
                mii.cbSize = 48;
                mii.fMask = (uint) MIIM.TYPE | (uint)MIIM.STATE | (uint) MIIM.SUBMENU;
                mii.hSubMenu = (int) hmnuPopup;
                mii.fType = (uint) MF.STRING;
                mii.dwTypeData = "Calculate File Hash";
                mii.fState = (uint) MF.ENABLED;
                Helpers.InsertMenuItem(hMenu, (uint)iMenu, 1, ref mii);

                // Add a separator
                MENUITEMINFO sep = new MENUITEMINFO();
                sep.cbSize = 48;
                sep.fMask = (uint )MIIM.TYPE;
                sep.fType = (uint) MF.SEPARATOR;
                Helpers.InsertMenuItem(hMenu, iMenu+1, 1, ref sep);

            }
            return id;
        }
		private uint InsertMenuItemPrivate( IntPtr menuHandle, string textName, int position, RightClickActionDelegate actionDelegate )
		{
			if ( mMenuItemsAddedCount >= mMenuMaximumItemsWeCanAdd )
				return 0;

			// Create a new Menu Item to add to the popup menu
			MENUITEMINFO mii = new MENUITEMINFO();
			mii.cbSize = ( uint )Marshal.SizeOf( typeof( MENUITEMINFO ) );
			mii.fMask = ( uint )MIIM.ID | ( uint )MIIM.TYPE | ( uint )MIIM.STATE;
			mii.wID = ( uint )( mMenuCmdFirst + mMenuItemsAddedCount );
			mii.fType = ( uint )MF.STRING;
			mii.dwTypeData = textName;
			mii.cch = 0;
			mii.fState = ( uint )MF.ENABLED;

			// Add it to the item
			if ( DllImports.InsertMenuItem( menuHandle, (uint)position, true, ref mii ) == 0 )
			{
				// failed
				//int error = Marshal.GetLastWin32Error();
				//System.Windows.Forms.MessageBox.Show( "InsertMenuItem failed + 0x" + error.ToString( "X" ) + " : " + ( new System.ComponentModel.Win32Exception( error ) ).Message );
				return 0;
			}
			else
			{
				mMenuIdsMap.Add( mMenuItemsAddedCount, actionDelegate );
				mMenuItemsAddedCount++;
				return mii.wID;
			}
		}
Example #58
0
 void AddMenuItem(uint hMenu, string text, int id, uint position)
 {
     MENUITEMINFO mii = new MENUITEMINFO();
     mii.cbSize = 48;
     mii.fMask = (uint)MIIM.ID | (uint)MIIM.TYPE | (uint)MIIM.STATE;
     mii.wID	= id;
     mii.fType = (uint)MF.STRING;
     mii.dwTypeData	= text;
     mii.fState = (uint)MF.ENABLED;
     Helpers.InsertMenuItem(hMenu, position, 1, ref mii);
 }
Example #59
0
 public static extern bool InsertMenuItem(
     IntPtr hMenu,
     uint uItem,
     [MarshalAs(UnmanagedType.Bool)]bool fByPosition,
     ref MENUITEMINFO mii);
Example #60
0
 //add a separator to the menu
 public static void AddSeparator(uint hMenu, uint position)
 {
     // Add a separator
     MENUITEMINFO sep = new MENUITEMINFO();
     sep.cbSize = 48;
     sep.fMask = (uint )MIIM.TYPE;
     sep.fType = (uint) MF.SEPARATOR;
     MenuHelper.InsertMenuItem(hMenu, position, 1, ref sep);
 }