GetMenuItemInfo, but i´m getting a 1456 - “Menu item not found”

夙愿已清 提交于 2019-12-13 09:15:01

问题


im trying to know if a menuItem is disabled or enabled, but i´m getting a 1456 - "Menu item not found" what am I doing wrong

in the first part is the declaration of the win32 libraries.

menuIndex is a parameter int submenuIndex is another parameter int

    [StructLayout(LayoutKind.Sequential)]
    struct MENUITEMINFO
    {
        public uint cbSize;
        public uint fMask;
        public uint fType;
        public uint fState;
        public uint wID;
        public IntPtr hSubMenu;
        public IntPtr hbmpChecked;
        public IntPtr hbmpUnchecked;
        public IntPtr dwItemData;
        public string dwTypeData;
        public uint cch;
        public IntPtr hbmpItem;

        // return the size of the structure
        public static uint sizeOf
        {
            get { return (uint)Marshal.SizeOf(typeof(MENUITEMINFO)); }
        }
    }

[DllImport("user32.dll")]
private static extern IntPtr GetMenu(IntPtr hWnd);

[DllImport("user32.dll")]
private static extern IntPtr GetSubMenu(IntPtr hMenu, int nPos);

[DllImport("user32.dll")]
private static extern uint GetMenuItemID(IntPtr hMenu, int nPos);

[DllImport("user32.dll", SetLastError = true)]
private static extern bool GetMenuItemInfo(IntPtr hMenu, int uItem, bool fByPosition, ref MENUITEMINFO lpmii);

....

IntPtr menu = GetMenu(handle);

IntPtr subMenu = GetSubMenu(menu, menuIndex);

uint menuItemID = GetMenuItemID(subMenu, submenuIndex);

MENUITEMINFO itemInfo = new MENUITEMINFO();
uint MIIM_STATE = 0x00000001;
itemInfo.cbSize = MENUITEMINFO.sizeOf;
itemInfo.fMask = MIIM_STATE;

if (!GetMenuItemInfo(menu, (int)submenuIndex, false, ref itemInfo))
{
    uint erro = GetLastError();
    //erro = 1456
    throw new Exception("Ocorreu um erro ao obter informações do Menu Centura - Cod: "+Marshal.GetLastWin32Error().ToString() +"\n http://msdn.microsoft.com/en-us/library/windows/desktop/ms681381(v=vs.85).aspx");
}                    

if (itemInfo.fState == MFS_DISABLED)
    throw new Exception("Disabled");

PostMessage(handle, 0x0111, (IntPtr)menuItemID, IntPtr.Zero);

回答1:


You are passing false for the fByPosition argument, so you need to pass a menu ID (menuItemID), not an index (submenuIndex). You also need to pass a handle to the menu that contains the item (subMenu, not menu).

The documentation says

fByPosition [in]

Type: BOOL

The meaning of uItem. If this parameter is FALSE, uItem is a menu item identifier. Otherwise, it is a menu item position. See Accessing Menu Items Programmatically for more information.

Either of these might work:

GetMenuItemInfo(subMenu, (int)submenuIndex, true, ref itemInfo)

GetMenuItemInfo(subMenu, (int)menuItemID, false, ref itemInfo)


来源:https://stackoverflow.com/questions/26472880/getmenuiteminfo-but-i%c2%b4m-getting-a-1456-menu-item-not-found

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!