private Image GetNativeMenuItemImage() { if ((this.nativeMenuCommandID == -1) || (this.nativeMenuHandle == IntPtr.Zero)) { return(null); } System.Windows.Forms.NativeMethods.MENUITEMINFO_T_RW lpmii = new System.Windows.Forms.NativeMethods.MENUITEMINFO_T_RW { fMask = 0x80, fType = 0x80, wID = this.nativeMenuCommandID }; System.Windows.Forms.UnsafeNativeMethods.GetMenuItemInfo(new HandleRef(this, this.nativeMenuHandle), this.nativeMenuCommandID, false, lpmii); if ((lpmii.hbmpItem != IntPtr.Zero) && (lpmii.hbmpItem.ToInt32() > 11)) { return(Image.FromHbitmap(lpmii.hbmpItem)); } int num = -1; switch (lpmii.hbmpItem.ToInt32()) { case 2: case 9: num = 3; break; case 3: case 7: case 11: num = 1; break; case 5: case 6: case 8: num = 0; break; case 10: num = 2; break; } if (num <= -1) { return(null); } Bitmap image = new Bitmap(0x10, 0x10); using (Graphics graphics = Graphics.FromImage(image)) { ControlPaint.DrawCaptionButton(graphics, new Rectangle(Point.Empty, image.Size), (CaptionButton)num, ButtonState.Flat); graphics.DrawRectangle(SystemPens.Control, 0, 0, image.Width - 1, image.Height - 1); } image.MakeTransparent(SystemColors.Control); return(image); }
private bool GetNativeMenuItemEnabled() { if ((this.nativeMenuCommandID == -1) || (this.nativeMenuHandle == IntPtr.Zero)) { return(false); } System.Windows.Forms.NativeMethods.MENUITEMINFO_T_RW lpmii = new System.Windows.Forms.NativeMethods.MENUITEMINFO_T_RW { cbSize = Marshal.SizeOf(typeof(System.Windows.Forms.NativeMethods.MENUITEMINFO_T_RW)), fMask = 1, fType = 1, wID = this.nativeMenuCommandID }; System.Windows.Forms.UnsafeNativeMethods.GetMenuItemInfo(new HandleRef(this, this.nativeMenuHandle), this.nativeMenuCommandID, false, lpmii); return((lpmii.fState & 3) == 0); }
internal static ToolStripDropDownMenu FromHMenu(IntPtr hmenu, IWin32Window targetWindow) { ToolStripDropDownMenu menu = new ToolStripDropDownMenu(); menu.SuspendLayout(); HandleRef hMenu = new HandleRef(null, hmenu); int menuItemCount = System.Windows.Forms.UnsafeNativeMethods.GetMenuItemCount(hMenu); for (int i = 0; i < menuItemCount; i++) { ToolStripItem item; System.Windows.Forms.NativeMethods.MENUITEMINFO_T_RW lpmii = new System.Windows.Forms.NativeMethods.MENUITEMINFO_T_RW { cbSize = Marshal.SizeOf(typeof(System.Windows.Forms.NativeMethods.MENUITEMINFO_T_RW)), fMask = 0x100, fType = 0x100 }; System.Windows.Forms.UnsafeNativeMethods.GetMenuItemInfo(hMenu, i, true, lpmii); if (lpmii.fType == 0x800) { item = new ToolStripSeparator(); } else { lpmii = new System.Windows.Forms.NativeMethods.MENUITEMINFO_T_RW { cbSize = Marshal.SizeOf(typeof(System.Windows.Forms.NativeMethods.MENUITEMINFO_T_RW)), fMask = 2, fType = 2 }; System.Windows.Forms.UnsafeNativeMethods.GetMenuItemInfo(hMenu, i, true, lpmii); item = new ToolStripMenuItem(hmenu, lpmii.wID, targetWindow); lpmii = new System.Windows.Forms.NativeMethods.MENUITEMINFO_T_RW { cbSize = Marshal.SizeOf(typeof(System.Windows.Forms.NativeMethods.MENUITEMINFO_T_RW)), fMask = 4, fType = 4 }; System.Windows.Forms.UnsafeNativeMethods.GetMenuItemInfo(hMenu, i, true, lpmii); if (lpmii.hSubMenu != IntPtr.Zero) { ((ToolStripMenuItem)item).DropDown = FromHMenu(lpmii.hSubMenu, targetWindow); } } menu.Items.Add(item); } menu.ResumeLayout(); return(menu); }
private string GetNativeMenuItemTextAndShortcut() { if ((this.nativeMenuCommandID == -1) || (this.nativeMenuHandle == IntPtr.Zero)) { return(null); } string str = null; System.Windows.Forms.NativeMethods.MENUITEMINFO_T_RW lpmii = new System.Windows.Forms.NativeMethods.MENUITEMINFO_T_RW { fMask = 0x40, fType = 0x40, wID = this.nativeMenuCommandID, dwTypeData = IntPtr.Zero }; System.Windows.Forms.UnsafeNativeMethods.GetMenuItemInfo(new HandleRef(this, this.nativeMenuHandle), this.nativeMenuCommandID, false, lpmii); if (lpmii.cch > 0) { lpmii.cch++; lpmii.wID = this.nativeMenuCommandID; IntPtr ptr = Marshal.AllocCoTaskMem(lpmii.cch * Marshal.SystemDefaultCharSize); lpmii.dwTypeData = ptr; try { System.Windows.Forms.UnsafeNativeMethods.GetMenuItemInfo(new HandleRef(this, this.nativeMenuHandle), this.nativeMenuCommandID, false, lpmii); if (lpmii.dwTypeData != IntPtr.Zero) { str = Marshal.PtrToStringAuto(lpmii.dwTypeData, lpmii.cch); } } finally { if (ptr != IntPtr.Zero) { Marshal.FreeCoTaskMem(ptr); } } } return(str); }
private string GetNativeMenuItemTextAndShortcut() { if ((this.nativeMenuCommandID == -1) || (this.nativeMenuHandle == IntPtr.Zero)) { return null; } string str = null; System.Windows.Forms.NativeMethods.MENUITEMINFO_T_RW lpmii = new System.Windows.Forms.NativeMethods.MENUITEMINFO_T_RW { fMask = 0x40, fType = 0x40, wID = this.nativeMenuCommandID, dwTypeData = IntPtr.Zero }; System.Windows.Forms.UnsafeNativeMethods.GetMenuItemInfo(new HandleRef(this, this.nativeMenuHandle), this.nativeMenuCommandID, false, lpmii); if (lpmii.cch > 0) { lpmii.cch++; lpmii.wID = this.nativeMenuCommandID; IntPtr ptr = Marshal.AllocCoTaskMem(lpmii.cch * Marshal.SystemDefaultCharSize); lpmii.dwTypeData = ptr; try { System.Windows.Forms.UnsafeNativeMethods.GetMenuItemInfo(new HandleRef(this, this.nativeMenuHandle), this.nativeMenuCommandID, false, lpmii); if (lpmii.dwTypeData != IntPtr.Zero) { str = Marshal.PtrToStringAuto(lpmii.dwTypeData, lpmii.cch); } } finally { if (ptr != IntPtr.Zero) { Marshal.FreeCoTaskMem(ptr); } } } return str; }
private Image GetNativeMenuItemImage() { if ((this.nativeMenuCommandID == -1) || (this.nativeMenuHandle == IntPtr.Zero)) { return null; } System.Windows.Forms.NativeMethods.MENUITEMINFO_T_RW lpmii = new System.Windows.Forms.NativeMethods.MENUITEMINFO_T_RW { fMask = 0x80, fType = 0x80, wID = this.nativeMenuCommandID }; System.Windows.Forms.UnsafeNativeMethods.GetMenuItemInfo(new HandleRef(this, this.nativeMenuHandle), this.nativeMenuCommandID, false, lpmii); if ((lpmii.hbmpItem != IntPtr.Zero) && (lpmii.hbmpItem.ToInt32() > 11)) { return Image.FromHbitmap(lpmii.hbmpItem); } int num = -1; switch (lpmii.hbmpItem.ToInt32()) { case 2: case 9: num = 3; break; case 3: case 7: case 11: num = 1; break; case 5: case 6: case 8: num = 0; break; case 10: num = 2; break; } if (num <= -1) { return null; } Bitmap image = new Bitmap(0x10, 0x10); using (Graphics graphics = Graphics.FromImage(image)) { ControlPaint.DrawCaptionButton(graphics, new Rectangle(Point.Empty, image.Size), (CaptionButton) num, ButtonState.Flat); graphics.DrawRectangle(SystemPens.Control, 0, 0, image.Width - 1, image.Height - 1); } image.MakeTransparent(SystemColors.Control); return image; }
private bool GetNativeMenuItemEnabled() { if ((this.nativeMenuCommandID == -1) || (this.nativeMenuHandle == IntPtr.Zero)) { return false; } System.Windows.Forms.NativeMethods.MENUITEMINFO_T_RW lpmii = new System.Windows.Forms.NativeMethods.MENUITEMINFO_T_RW { cbSize = Marshal.SizeOf(typeof(System.Windows.Forms.NativeMethods.MENUITEMINFO_T_RW)), fMask = 1, fType = 1, wID = this.nativeMenuCommandID }; System.Windows.Forms.UnsafeNativeMethods.GetMenuItemInfo(new HandleRef(this, this.nativeMenuHandle), this.nativeMenuCommandID, false, lpmii); return ((lpmii.fState & 3) == 0); }
internal static ToolStripDropDownMenu FromHMenu(IntPtr hmenu, IWin32Window targetWindow) { ToolStripDropDownMenu menu = new ToolStripDropDownMenu(); menu.SuspendLayout(); HandleRef hMenu = new HandleRef(null, hmenu); int menuItemCount = System.Windows.Forms.UnsafeNativeMethods.GetMenuItemCount(hMenu); for (int i = 0; i < menuItemCount; i++) { ToolStripItem item; System.Windows.Forms.NativeMethods.MENUITEMINFO_T_RW lpmii = new System.Windows.Forms.NativeMethods.MENUITEMINFO_T_RW { cbSize = Marshal.SizeOf(typeof(System.Windows.Forms.NativeMethods.MENUITEMINFO_T_RW)), fMask = 0x100, fType = 0x100 }; System.Windows.Forms.UnsafeNativeMethods.GetMenuItemInfo(hMenu, i, true, lpmii); if (lpmii.fType == 0x800) { item = new ToolStripSeparator(); } else { lpmii = new System.Windows.Forms.NativeMethods.MENUITEMINFO_T_RW { cbSize = Marshal.SizeOf(typeof(System.Windows.Forms.NativeMethods.MENUITEMINFO_T_RW)), fMask = 2, fType = 2 }; System.Windows.Forms.UnsafeNativeMethods.GetMenuItemInfo(hMenu, i, true, lpmii); item = new ToolStripMenuItem(hmenu, lpmii.wID, targetWindow); lpmii = new System.Windows.Forms.NativeMethods.MENUITEMINFO_T_RW { cbSize = Marshal.SizeOf(typeof(System.Windows.Forms.NativeMethods.MENUITEMINFO_T_RW)), fMask = 4, fType = 4 }; System.Windows.Forms.UnsafeNativeMethods.GetMenuItemInfo(hMenu, i, true, lpmii); if (lpmii.hSubMenu != IntPtr.Zero) { ((ToolStripMenuItem) item).DropDown = FromHMenu(lpmii.hSubMenu, targetWindow); } } menu.Items.Add(item); } menu.ResumeLayout(); return menu; }