Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
382 views
in Technique[技术] by (71.8m points)

c# - GetMenuItemInfo, but i´m getting a 1456 - "Menu item not found"

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() +"
 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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...