/// <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); }
/// <include file='doc\Menu.uex' path='docs/doc[@for="Menu.DestroyMenuItems"]/*' /> /// <devdoc> /// </devdoc> /// <internalonly/> internal void DestroyMenuItems() { if (created) { while (UnsafeNativeMethods.GetMenuItemCount(new HandleRef(this, handle)) > 0) { UnsafeNativeMethods.RemoveMenu(new HandleRef(this, handle), 0, NativeMethods.MF_BYPOSITION); } created = false; } }
/// <include file='doc\Menu.uex' path='docs/doc[@for="Menu.DestroyMenuItems"]/*' /> /// <devdoc> /// </devdoc> /// <internalonly/> internal void DestroyMenuItems() { if (created) { for (int i = 0; i < ItemCount; i++) { items[i].ClearHandles(); } while (UnsafeNativeMethods.GetMenuItemCount(new HandleRef(this, handle)) > 0) { UnsafeNativeMethods.RemoveMenu(new HandleRef(this, handle), 0, NativeMethods.MF_BYPOSITION); } created = false; } }