示例#1
0
        internal static Menu[] GetMenuArray(IntPtr mHwnd)
        {
            List <Menu> list = new List <Menu>();

            int count = WinAPI.GetMenuItemCount(mHwnd);

            for (uint i = 0; i < count; i++)
            {
                WinAPI.MENUITEMINFO mif = new WinAPI.MENUITEMINFO(WinAPI.MIIM.STRING | WinAPI.MIIM.ID | WinAPI.MIIM.SUBMENU);
                if (!WinAPI.GetMenuItemInfo(mHwnd, i, true, mif))
                {
                    throw new Exception(string.Format("WinAPI.GetMenuItemInfo failed! index:[{0}]", i));
                }

                mif.cch++;
                mif.dwTypeData = Marshal.AllocHGlobal((IntPtr)(mif.cch * 2));
                try
                {
                    if (!WinAPI.GetMenuItemInfo(mHwnd, i, true, mif))
                    {
                        throw new Exception(string.Format("WinAPI.GetMenuItemInfo failed! index:[{0}]", i));
                    }
                    string caption = Marshal.PtrToStringUni(mif.dwTypeData);;


                    list.Add(new Menu(GetMenuItemID(mHwnd, (int)i), mHwnd, mif.hSubMenu, IntPtr.Zero, caption.Replace("&", "")));
                }
                finally
                {
                    Marshal.FreeHGlobal(mif.dwTypeData);
                }
            }
            return(list.ToArray());
        }