Ejemplo n.º 1
0
 public static extern bool GetMenuItemInfo(IntPtr hMenu, uint uItem, [MarshalAs(UnmanagedType.Bool)] bool fByPosition, ref MENUITEMINFO lpmii);
 private void InitializeToolStripItems(ToolStripItemCollection itemCollection, IntPtr menu)
 {
     itemCollection.Clear();
     int menuItemCount = Windows.GetMenuItemCount(menu);
     MENUITEMINFO lpmii = new MENUITEMINFO {
         cbSize = MENUITEMINFO.SizeOf
     };
     for (uint i = 0; i < menuItemCount; i++)
     {
         ToolStripItem item;
         lpmii.fMask = MIIM.MIIM_DATA | MIIM.MIIM_STRING | MIIM.MIIM_FTYPE | MIIM.MIIM_BITMAP | MIIM.MIIM_CHECKMARKS | MIIM.MIIM_STATE | MIIM.MIIM_SUBMENU | MIIM.MIIM_ID;
         lpmii.dwTypeData = null;
         Windows.GetMenuItemInfo(menu, i, true, ref lpmii);
         if ((lpmii.fType & MFT.MFT_SEPARATOR) > MFT.MFT_STRING)
         {
             item = new ToolStripSeparator();
         }
         else
         {
             item = new ToolStripMenuItem();
             if (lpmii.cch > 0)
             {
                 lpmii.cch++;
                 lpmii.dwTypeData = new string(' ', (int) lpmii.cch);
                 Windows.GetMenuItemInfo(menu, i, true, ref lpmii);
                 item.Text = lpmii.dwTypeData;
             }
             Image image = null;
             if (((lpmii.fMask & MIIM.MIIM_BITMAP) > ((MIIM) 0)) && (lpmii.hbmpItem != IntPtr.Zero))
             {
                 if (lpmii.hbmpItem == Windows.HBMMENU_CALLBACK)
                 {
                     lpmii.fType |= MFT.MFT_OWNERDRAW;
                 }
                 else
                 {
                     image = ImageHelper.FromHbitmapWithAlpha(lpmii.hbmpItem);
                     image.RotateFlip(RotateFlipType.Rotate180FlipX);
                 }
             }
             else if (((lpmii.fMask & MIIM.MIIM_CHECKMARKS) > ((MIIM) 0)) && (lpmii.hbmpUnchecked != IntPtr.Zero))
             {
                 image = ImageHelper.FromHbitmapWithAlpha(lpmii.hbmpUnchecked);
                 item.ImageTransparentColor = SystemColors.Window;
             }
             if (image != null)
             {
                 item.ImageScaling = ToolStripItemImageScaling.None;
                 item.Image = image;
             }
             if (((lpmii.fType & MFT.MFT_OWNERDRAW) > MFT.MFT_STRING) && ((this.ContextMenu3 != null) || (this.ContextMenu2 != null)))
             {
                 item.Paint += new PaintEventHandler(this.ToolStripMenuItem_Paint);
             }
             if (lpmii.hSubMenu != IntPtr.Zero)
             {
                 ToolStripMenuItem item2 = (ToolStripMenuItem) item;
                 item2.DropDownItemClicked += new ToolStripItemClickedEventHandler(this.ToolStrip_ItemClick);
                 if ((this.ContextMenu3 != null) || (this.ContextMenu2 != null))
                 {
                     item2.DropDownItems.Add(string.Empty);
                     item2.DropDownOpening += new EventHandler(this.ToolStripMenuItem_DropDownOpening);
                 }
                 else
                 {
                     this.InitializeToolStripItems(item2.DropDownItems, lpmii.hSubMenu);
                 }
             }
         }
         item.Tag = new MenuItemInfo { Info = lpmii, Menu = menu, Index = i };
         itemCollection.Add(item);
         this.ItemContainer.Add(item);
     }
 }