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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…