/// <devdoc> this takes a native menu and builds up a managed toolstrip around it. /// Scenario: showing the items from the SystemMenu. /// targetWindow is the window to send WM_COMMAND, WM_SYSCOMMAND to /// hmenu is a handle to the native menu /// /// internal static ToolStripDropDownMenu FromHMenu(IntPtr hmenu, IWin32Window targetWindow) { ToolStripDropDownMenu managedDropDown = new ToolStripDropDownMenu(); managedDropDown.SuspendLayout(); HandleRef menuHandle = new HandleRef(null, hmenu); int count = UnsafeNativeMethods.GetMenuItemCount(menuHandle); ToolStripItem itemToAdd; // surf through the items in the collection, building up TSMenuItems and TSSeparators // corresponding to the native menu. for (int i = 0; i < count; i++) { // peek at the i'th item. NativeMethods.MENUITEMINFO_T_RW info = new NativeMethods.MENUITEMINFO_T_RW(); info.cbSize = Marshal.SizeOf <NativeMethods.MENUITEMINFO_T_RW>(); info.fMask = NativeMethods.MIIM_FTYPE; info.fType = NativeMethods.MIIM_FTYPE; UnsafeNativeMethods.GetMenuItemInfo(menuHandle, i, /*fByPosition=*/ true, info); if (info.fType == NativeMethods.MFT_SEPARATOR) { // its a separator. itemToAdd = new ToolStripSeparator(); } else { // its a menu item... lets fish out the command id info = new NativeMethods.MENUITEMINFO_T_RW(); info.cbSize = Marshal.SizeOf <NativeMethods.MENUITEMINFO_T_RW>(); info.fMask = NativeMethods.MIIM_ID; info.fType = NativeMethods.MIIM_ID; UnsafeNativeMethods.GetMenuItemInfo(menuHandle, i, /*fByPosition=*/ true, info); // create the managed object - toolstripmenu item knows how to grok hmenu for information. itemToAdd = new ToolStripMenuItem(hmenu, info.wID, targetWindow); // if there is a submenu fetch it. info = new NativeMethods.MENUITEMINFO_T_RW(); info.cbSize = Marshal.SizeOf <NativeMethods.MENUITEMINFO_T_RW>(); info.fMask = NativeMethods.MIIM_SUBMENU; info.fType = NativeMethods.MIIM_SUBMENU; UnsafeNativeMethods.GetMenuItemInfo(menuHandle, i, /*fByPosition=*/ true, info); if (info.hSubMenu != IntPtr.Zero) { // set the dropdown to be the items from the submenu ((ToolStripMenuItem)itemToAdd).DropDown = FromHMenu(info.hSubMenu, targetWindow); } } managedDropDown.Items.Add(itemToAdd); } managedDropDown.ResumeLayout(); return(managedDropDown); }
public static BOOL GetMenuItemInfo(HandleRef hMenu, int uItem, bool fByPosition, NativeMethods.MENUITEMINFO_T_RW lpmii) { BOOL result = GetMenuItemInfo(hMenu.Handle, uItem, fByPosition, lpmii); GC.KeepAlive(hMenu.Wrapper); return(result); }
public static extern BOOL GetMenuItemInfo(IntPtr hMenu, int uItem, bool fByPosition, [In, Out] NativeMethods.MENUITEMINFO_T_RW lpmii);
/// <devdoc> this takes a native menu and builds up a managed toolstrip around it. /// Scenario: showing the items from the SystemMenu. /// targetWindow is the window to send WM_COMMAND, WM_SYSCOMMAND to /// hmenu is a handle to the native menu /// /// internal static ToolStripDropDownMenu FromHMenu(IntPtr hmenu, IWin32Window targetWindow) { ToolStripDropDownMenu managedDropDown = new ToolStripDropDownMenu(); managedDropDown.SuspendLayout(); HandleRef menuHandle = new HandleRef(null, hmenu); int count = UnsafeNativeMethods.GetMenuItemCount(menuHandle); ToolStripItem itemToAdd; // surf through the items in the collection, building up TSMenuItems and TSSeparators // corresponding to the native menu. for (int i = 0; i < count; i++) { // peek at the i'th item. NativeMethods.MENUITEMINFO_T_RW info = new NativeMethods.MENUITEMINFO_T_RW(); info.cbSize = Marshal.SizeOf(typeof(NativeMethods.MENUITEMINFO_T_RW)); info.fMask = NativeMethods.MIIM_FTYPE; info.fType = NativeMethods.MIIM_FTYPE; UnsafeNativeMethods.GetMenuItemInfo(menuHandle, i, /*fByPosition=*/ true, info); if (info.fType == NativeMethods.MFT_SEPARATOR){ // its a separator. itemToAdd = new ToolStripSeparator(); } else { // its a menu item... lets fish out the command id info = new NativeMethods.MENUITEMINFO_T_RW(); info.cbSize = Marshal.SizeOf(typeof(NativeMethods.MENUITEMINFO_T_RW)); info.fMask = NativeMethods.MIIM_ID; info.fType = NativeMethods.MIIM_ID; UnsafeNativeMethods.GetMenuItemInfo(menuHandle, i, /*fByPosition=*/ true, info); // create the managed object - toolstripmenu item knows how to grok hmenu for information. itemToAdd = new ToolStripMenuItem(hmenu, info.wID, targetWindow); // if there is a submenu fetch it. info = new NativeMethods.MENUITEMINFO_T_RW(); info.cbSize = Marshal.SizeOf(typeof(NativeMethods.MENUITEMINFO_T_RW)); info.fMask = NativeMethods.MIIM_SUBMENU; info.fType = NativeMethods.MIIM_SUBMENU; UnsafeNativeMethods.GetMenuItemInfo(menuHandle, i, /*fByPosition=*/ true, info); if (info.hSubMenu != IntPtr.Zero) { // set the dropdown to be the items from the submenu ((ToolStripMenuItem)itemToAdd).DropDown = FromHMenu(info.hSubMenu, targetWindow); } } managedDropDown.Items.Add(itemToAdd); } managedDropDown.ResumeLayout(); return managedDropDown; }
private Image GetNativeMenuItemImage(){ if (nativeMenuCommandID == -1 || nativeMenuHandle == IntPtr.Zero) { Debug.Fail("why were we called to fetch native menu item info with nothing assigned?"); return null; } NativeMethods.MENUITEMINFO_T_RW info = new NativeMethods.MENUITEMINFO_T_RW(); info.fMask = NativeMethods.MIIM_BITMAP; info.fType = NativeMethods.MIIM_BITMAP; info.wID = nativeMenuCommandID; UnsafeNativeMethods.GetMenuItemInfo(new HandleRef(this, nativeMenuHandle), nativeMenuCommandID, /*fByPosition instead of ID=*/ false, info); if (info.hbmpItem != IntPtr.Zero && info.hbmpItem.ToInt32() > NativeMethods.HBMMENU_POPUP_MINIMIZE) { return Bitmap.FromHbitmap(info.hbmpItem); } else { // its a system defined bitmap int buttonToUse = -1; switch (info.hbmpItem.ToInt32()) { case NativeMethods.HBMMENU_MBAR_CLOSE: case NativeMethods.HBMMENU_MBAR_CLOSE_D: case NativeMethods.HBMMENU_POPUP_CLOSE: buttonToUse = (int)CaptionButton.Close; break; case NativeMethods.HBMMENU_MBAR_MINIMIZE: case NativeMethods.HBMMENU_MBAR_MINIMIZE_D: case NativeMethods.HBMMENU_POPUP_MINIMIZE: buttonToUse = (int)CaptionButton.Minimize; break; case NativeMethods.HBMMENU_MBAR_RESTORE: case NativeMethods.HBMMENU_POPUP_RESTORE: buttonToUse = (int)CaptionButton.Restore; break; case NativeMethods.HBMMENU_POPUP_MAXIMIZE: buttonToUse = (int)CaptionButton.Maximize; break; case NativeMethods.HBMMENU_SYSTEM: // case NativeMethods.HBMMENU_CALLBACK: // owner draw not supported default: break; } if (buttonToUse > -1) { // we've mapped to a system defined bitmap we know how to draw Bitmap image = new Bitmap(16, 16); using (Graphics g = Graphics.FromImage(image)) { ControlPaint.DrawCaptionButton(g, new Rectangle(Point.Empty, image.Size), (CaptionButton)buttonToUse, ButtonState.Flat); g.DrawRectangle(SystemPens.Control, 0, 0, image.Width - 1, image.Height - 1); } image.MakeTransparent(SystemColors.Control); return image; } } return null; }
// returns text and shortcut separated by tab. private string GetNativeMenuItemTextAndShortcut() { if (nativeMenuCommandID == -1 || nativeMenuHandle == IntPtr.Zero) { Debug.Fail("why were we called to fetch native menu item info with nothing assigned?"); return null; } string text = null; // fetch the string length NativeMethods.MENUITEMINFO_T_RW info = new NativeMethods.MENUITEMINFO_T_RW(); info.fMask = NativeMethods.MIIM_STRING; info.fType = NativeMethods.MIIM_STRING; info.wID = nativeMenuCommandID; info.dwTypeData = IntPtr.Zero; UnsafeNativeMethods.GetMenuItemInfo(new HandleRef(this, nativeMenuHandle), nativeMenuCommandID, /*fByPosition instead of ID=*/ false, info); if (info.cch > 0) { // fetch the string info.cch += 1; // according to MSDN we need to increment the count we receive by 1. info.wID = nativeMenuCommandID; IntPtr allocatedStringBuffer = Marshal.AllocCoTaskMem(info.cch * Marshal.SystemDefaultCharSize); info.dwTypeData = allocatedStringBuffer; try { UnsafeNativeMethods.GetMenuItemInfo(new HandleRef(this, nativeMenuHandle), nativeMenuCommandID, /*fByPosition instead of ID=*/ false, info); // convert the string into managed data. if (info.dwTypeData != IntPtr.Zero){ // we have to use PtrToStringAuto as we can't use Marshal.SizeOf to determine // the size of the struct with a StringBuilder member. text = Marshal.PtrToStringAuto(info.dwTypeData, info.cch); } } finally { if (allocatedStringBuffer != IntPtr.Zero) { // use our local instead of the info structure member *just* in case windows decides to clobber over it. // we want to be sure to deallocate the memory we know we allocated. Marshal.FreeCoTaskMem(allocatedStringBuffer); } } } return text; }
private bool GetNativeMenuItemEnabled() { if (nativeMenuCommandID == -1 || nativeMenuHandle == IntPtr.Zero) { Debug.Fail("why were we called to fetch native menu item info with nothing assigned?"); return false; } NativeMethods.MENUITEMINFO_T_RW info = new NativeMethods.MENUITEMINFO_T_RW(); info.cbSize = Marshal.SizeOf(typeof(NativeMethods.MENUITEMINFO_T_RW)); info.fMask = NativeMethods.MIIM_STATE; info.fType = NativeMethods.MIIM_STATE; info.wID = nativeMenuCommandID; UnsafeNativeMethods.GetMenuItemInfo(new HandleRef(this, nativeMenuHandle), nativeMenuCommandID, /*fByPosition instead of ID=*/ false, info); return ((info.fState & NativeMethods.MFS_DISABLED) == 0); }