我试图知道menuItem是否被禁用或启用,但我得到了1456 - “未找到菜单项” 我做错了什么
第一部分中的是win32库的声明。
menuIndex是一个参数int submenuIndex是另一个参数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);
答案 0 :(得分:3)
您正在为false
参数传递fByPosition
,因此您需要传递菜单ID(menuItemID
),而不是索引(submenuIndex
)。您还需要将句柄传递给包含该项目的菜单(subMenu
,而不是menu
)。
fByPosition
[in]
输入:
BOOL
uItem
的含义。如果此参数为FALSE
,则uItem
是菜单项标识符。否则,它是菜单项位置。有关详细信息,请参阅Accessing Menu Items Programmatically。
其中任何一个都可行:
GetMenuItemInfo(subMenu, (int)submenuIndex, true, ref itemInfo)
GetMenuItemInfo(subMenu, (int)menuItemID, false, ref itemInfo)