/// <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);
        }
Exemple #2
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);
        }
            /// <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;
            }
 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;
 }