public MenuItemInfo GetItemInfo(int commandId)
        {
            var result = MENUITEMINFO.Create();

            result.fMask = MIIM.MIIM_BITMAP;
            MenuNativeMethods.GetMenuItemInfo(this.popupMenu, (UInt32)commandId, false, ref result);
            return(MenuItemInfo.Create(result));
        }
Example #2
0
        private unsafe List <IWindowRef.MenuItemRef> GetMenuItems(IntPtr hMenu, IntPtr rootHandle)
        {
            // ref: https://stackoverflow.com/questions/18589385/retrieve-list-of-menu-items-in-windows-in-c
            // ref: https://stackoverflow.com/questions/22852461/win32-api-getmenuiteminfo-returns-only-the-first-character-of-the-item-text
            int nCount = GetMenuItemCount(hMenu);

            if (nCount <= 0)
            {
                return(null);
            }

            MenuMembersMask fMask = MenuMembersMask.MIIM_SUBMENU | MenuMembersMask.MIIM_FTYPE | MenuMembersMask.MIIM_ID | MenuMembersMask.MIIM_STRING;
            List <IWindowRef.MenuItemRef> list = new List <IWindowRef.MenuItemRef>();
            MENUITEMINFO mii = new MENUITEMINFO();

            for (uint i = 0; i < nCount; i++)
            {
                MENUITEMINFO mif = mii.Create();
                mif.dwTypeData = null;
                mif.fMask      = fMask;
                if (!GetMenuItemInfo(hMenu, i, true, ref mif))
                {
                    throw new Win32Exception();
                }

                switch (mif.fType)
                {
                case MenuItemType.MFT_SEPARATOR:
                    list.Add(new IWindowRef.MenuItemRef {
                        Index = i, IsSeparator = true
                    });
                    break;

                case MenuItemType.MFT_STRING:
                    if (mif.cch <= 0)
                    {
                        break;
                    }
                    //if (rootHandle == IntPtr.Zero)
                    //{
                    //    Debug.WriteLine("{0} is zero, default is {1}", rootHandle, hMenu);
                    //}
                    IWindowRef.MenuItemRef item = new IWindowRef.MenuItemRef
                    {
                        RootMenuHandle = rootHandle == IntPtr.Zero ? hMenu : rootHandle,
                        MenuHandle     = hMenu,
                        Ref            = mif.wID,
                        Index          = i,
                        IsSeparator    = false
                    };
                    mif.cch++;
                    IntPtr szString = Marshal.AllocHGlobal((IntPtr)(sizeof(char) * mif.cch));
                    //IntPtr szString = Marshal.StringToHGlobalUni(new string('\0', mif.cch));
                    mif.dwTypeData = (char *)szString.ToPointer();
                    try
                    {
                        if (!GetMenuItemInfo(hMenu, i, true, ref mif))
                        {
                            throw new Win32Exception();
                        }
                        item.Label = Marshal.PtrToStringUni(szString);
                    }
                    finally
                    {
                        Marshal.FreeHGlobal(szString);
                    }

                    if (mif.hSubMenu != null)
                    {
                        item.SubMenu = new IWindowRef.MenuRef
                        {
                            Ref   = mif.hSubMenu,
                            Items = GetMenuItems(mif.hSubMenu, item.RootMenuHandle)
                        };
                    }
                    list.Add(item);
                    break;

                default:
                    break;
                }
            }

            return(list);
        }