/// <summary> /// Format a given system error, or the last system error by default. /// </summary> /// <param name="syserror">Format code to pass. GetLastError is used as by default.</param> /// <returns></returns> /// <remarks></remarks> public static string FormatLastError(uint syserror = 0U) { uint err = syserror == 0L ? (uint)User32.GetLastError() : syserror; var mm = new SafePtr(); string s; mm.Length = 1026L; mm.ZeroMemory(); User32.FormatMessage(0x1000, IntPtr.Zero, err, 0U, mm.handle, 512U, IntPtr.Zero); s = "Error &H" + err.ToString("X8") + ": " + mm.ToString(); mm.Dispose(); return(s); }
public static string GetMenuItemText(IntPtr hMenu, int itemId, bool byPos = true) { var mii = new MENUITEMINFO(); var mm = new SafePtr(); mii.cbSize = Marshal.SizeOf(mii); mii.cch = 0; mii.fMask = User32.MIIM_TYPE; User32.GetMenuItemInfo(hMenu, itemId, byPos, ref mii); mm.Length = (mii.cch + 1) * sizeof(char); mii.cch += 1; mii.dwTypeData = mm.handle; if (User32.GetMenuItemInfo(hMenu, itemId, byPos, ref mii) == 0) { int err = User32.GetLastError(); mm.Length = 1026L; mm.ZeroMemory(); User32.FormatMessage(0x1000U, IntPtr.Zero, (uint)err, 0U, mm.handle, 512U, IntPtr.Zero); mm.Dispose(); return(null); } if ((mii.fType & User32.MFT_SEPARATOR) == User32.MFT_SEPARATOR) { return("-"); } else { string s; s = mm.ToString(); mm.Dispose(); return(s); } }
/// <summary> /// Copy a native hMenu and all its contents into a managed Menu object /// </summary> /// <param name="hMenu"></param> /// <param name="destItem"></param> /// <returns></returns> /// <remarks></remarks> public static bool MenuItemCopyToManaged(IntPtr hMenu, int itemId, ref ToolStripMenuItem destItem, bool byPos = true) { var mii = new MENUITEMINFO(); Bitmap bmp; var mm = new SafePtr(); if (destItem is null) { destItem = new ToolStripMenuItem(); } mii.cbSize = Marshal.SizeOf(mii); mii.cch = 0; mii.fMask = User32.MIIM_TYPE; User32.GetMenuItemInfo(hMenu, itemId, byPos, ref mii); mm.Length = (mii.cch + 1) * sizeof(char); mii.cch += 1; mii.dwTypeData = mm.handle; if (User32.GetMenuItemInfo(hMenu, itemId, byPos, ref mii) == 0) { int err = User32.GetLastError(); mm.Length = 1026L; mm.ZeroMemory(); User32.FormatMessage(0x1000U, IntPtr.Zero, (uint)err, 0U, mm.handle, 512U, IntPtr.Zero); mm.Dispose(); return(false); } if ((mii.fType & User32.MFT_SEPARATOR) == User32.MFT_SEPARATOR) { destItem.Text = "-"; } else { destItem.Text = mm.ToString(); } mm.Dispose(); mii.fMask = User32.MIIM_BITMAP; User32.GetMenuItemInfo(hMenu, itemId, byPos, ref mii); if (mii.hbmpItem != IntPtr.Zero) { bmp = Image.FromHbitmap(mii.hbmpItem); destItem.Image = bmp; } mii.fMask = User32.MIIM_STATE; User32.GetMenuItemInfo(hMenu, itemId, byPos, ref mii); if ((mii.fState & User32.MFS_CHECKED) == User32.MFS_CHECKED) { destItem.CheckState = CheckState.Checked; } if ((mii.fState & User32.MFS_DISABLED) == User32.MFS_DISABLED) { destItem.Enabled = false; } if ((mii.fState & User32.MFS_DEFAULT) == User32.MFS_DEFAULT) { destItem.Font = new Font(destItem.Font.Name, destItem.Font.Size, FontStyle.Bold); } mii.fMask = User32.MIIM_SUBMENU; User32.GetMenuItemInfo(hMenu, itemId, byPos, ref mii); if (mii.hSubMenu != IntPtr.Zero) { return(MenuItemsToManaged(mii.hSubMenu, destItem.DropDownItems)); } return(true); }