Ejemplo n.º 1
0
        public void SetItems(PopupMenuItem[] items)
        {
            menuItems   = items;
            itemsBounds = new Rectangle[menuItems.Length];
            int      width    = 0;
            int      height   = BORDER;
            Graphics graphics = this.CreateGraphics();
            int      index    = 0;

            foreach (MenuItem item in menuItems)
            {
                if (item.IsParent)
                {
                    CreateSubMenu(item);
                }
                MeasureItemEventArgs ev =
                    new MeasureItemEventArgs(graphics, index);
                PopupMenuItem.PopupMenu_MeasureItem(item, ev);
                itemsBounds[index] = new Rectangle(BORDER, height, ev.ItemWidth, ev.ItemHeight);
                width   = Math.Max(width, ev.ItemWidth);
                height += ev.ItemHeight;
                index++;
            }
            width += 10;
            for (int i = 0; i < itemsBounds.Length; i++)
            {
                itemsBounds[i].Width = width;
            }
            this.Size = new Size(width + BORDER * 2, height + BORDER);
        }
Ejemplo n.º 2
0
        private void ShowSubMenu(PopupMenuItem parentItem)
        {
            FlatPopupMenu subMenu = (FlatPopupMenu)subMenus[parentItem];

            if (subMenu == null)
            {
                return;
            }
            subMenu.Show();
            subMenu.Left = this.Left + this.Width;
            subMenu.Top  = this.Top + itemsBounds[SelectedIndex].Top - 1;
            subMenu.BringToFront();
        }
Ejemplo n.º 3
0
 protected override void OnMouseHover(EventArgs e)
 {
     base.OnMouseHover(e);
     base.ResetMouseEventArgs();
     //Show submenu when mouse hover
     if (SelectedIndex > -1)
     {
         PopupMenuItem parentItem = menuItems[SelectedIndex];
         if (!parentItem.IsParent || !parentItem.Enabled)
         {
             return;
         }
         ShowSubMenu(parentItem);
     }
 }
Ejemplo n.º 4
0
        private void CreateSubMenu(MenuItem parentItem)
        {
            if (parentItem.MenuItems == null || parentItem.MenuItems.Count == 0)
            {
                return;
            }
            FlatPopupMenu childMenu = new FlatPopupMenu();

            PopupMenuItem[] items = new PopupMenuItem[parentItem.MenuItems.Count];
            foreach (MenuItem item in parentItem.MenuItems)
            {
                items[parentItem.MenuItems.IndexOf(item)] = (PopupMenuItem)item;
            }
            childMenu.SetItems(items);
            this.Parent.Controls.Add(childMenu);
            subMenus.Add(parentItem, childMenu);
        }
Ejemplo n.º 5
0
        protected override void OnPaint(PaintEventArgs e)
        {
            //Draw background
            SolidBrush brush = new SolidBrush(this.BackColor);

            e.Graphics.FillRectangle(brush, 0, 0, this.Width, this.Height);
            //Draw Items
            if (menuItems != null && menuItems.Length > 0)
            {
                int index = 0;
                foreach (MenuItem item in menuItems)
                {
                    DrawItemState state = DrawItemState.None;
                    if (item.Checked)
                    {
                        state |= DrawItemState.Checked;
                    }
                    if (selectedIndex != -1 && item == menuItems[selectedIndex])
                    {
                        state |= DrawItemState.Selected;
                    }
                    DrawItemEventArgs ev =
                        new DrawItemEventArgs(e.Graphics, SystemInformation.MenuFont,
                                              itemsBounds[index], index, state);
                    PopupMenuItem.PopupMenu_DrawItem(item, ev, true);

                    index++;
                }
            }
            //Draw border
            Color innerColor  = Color.FromArgb(238, 236, 255);
            Color cornerColor = Color.FromArgb(189, 182, 255);
            Color outerColor  = Color.FromArgb(47, 37, 181);
            Point p1          = new Point(0, this.Height - 1);
            Point p2          = new Point(0, 0);
            Point p3          = new Point(this.Width - 1, 0);
            Point p4          = new Point(this.Width - 1, this.Height - 1);
            Pen   pen         = new Pen(cornerColor, 1);

            e.Graphics.DrawRectangle(pen, p2.X, p2.Y, p4.X, p4.Y);
            pen.Color = innerColor;
            e.Graphics.DrawLines(pen, new Point[] { new Point(p1.X, p1.Y - 1), p2, new Point(p3.X - 1, p3.Y) });
            pen.Color = outerColor;
            e.Graphics.DrawLines(pen, new Point[] { new Point(p1.X + 1, p1.Y), p4, new Point(p3.X, p3.Y + 1) });
        }
Ejemplo n.º 6
0
        protected override void OnMouseDown(MouseEventArgs e)
        {
            //Show submenu when mouse hover
            PopupMenuItem parentItem = menuItems[SelectedIndex];

            if (parentItem.IsParent)
            {
                if (parentItem.Enabled)
                {
                    ShowSubMenu(parentItem);
                }
                return;
            }
            //Process click event
            if (e.Button != MouseButtons.Left)
            {
                return;
            }
            int index = 0;

            foreach (Rectangle rect in itemsBounds)
            {
                if (rect.Contains(e.X, e.Y))
                {
                    if (menuItems[index].Enabled && menuItems[index].Text != "-")
                    {
                        if (OwnerMenu != null)
                        {
                            OwnerMenu.Selected = false;
                        }
                        menuItems[index].ProcessClick(new EventArgs());
                    }
                    break;
                }
                index++;
            }
        }
Ejemplo n.º 7
0
        public static void PopupMenu_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e, bool withArrows)
        {
            PopupMenuItem item    = (PopupMenuItem)sender;
            string        caption = item.Text;

            //Paint background
            Color      backColor = item.BackColor;
            SolidBrush brush     = new SolidBrush(Settings.MenuItemBackColor);

            e.Graphics.FillRectangle(brush, e.Bounds.Left, e.Bounds.Top, e.Bounds.Width, e.Bounds.Height);


            if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
            {
                brush.Color = Settings.MenuItemSelectedBackColor;
                e.Graphics.FillRectangle(brush, e.Bounds.Left + 1, e.Bounds.Top + 3, e.Bounds.Width - 1, e.Bounds.Height - 6);
            }

            //Draw text
            if (item.Text != "-")
            {
                int horizOffset = 16;
                if (item.Enabled)
                {
                    if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
                    {
                        brush.Color = Settings.MenuItemSelectedForeColor;
                    }
                    else
                    {
                        brush.Color = Settings.MenuItemForeColor;
                    }
                }
                else
                {
                    brush.Color = Settings.MenuItemDisabledForeColor;
                }

                StringFormat sf = new StringFormat();
                sf.HotkeyPrefix  = HotkeyPrefix.Hide;
                sf.Alignment     = StringAlignment.Near;
                sf.LineAlignment = StringAlignment.Center;
                Rectangle textRect = new Rectangle(
                    e.Bounds.Left + horizOffset, e.Bounds.Top,
                    e.Bounds.Width - horizOffset, e.Bounds.Height);
                e.Graphics.DrawString(caption, e.Font, brush, textRect, sf);

                if (item.Image != null)
                {
                    //e.Graphics.DrawImageUnscaled(item.Image, e.Bounds.Location.X, e.Bounds.Location.Y);
                    e.Graphics.DrawImage(item.Image, e.Bounds.Location.X, e.Bounds.Location.Y + 1, item.Image.Width, item.Image.Height);
                    //item.Image.Save("c:\\1.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
                }
            }
            else
            {
                //Draw separator line
                Pen   pen = new Pen(Color.White, 1);
                Point src = new Point(e.Bounds.Left, e.Bounds.Top + 1);
                Point dst = new Point(e.Bounds.Left + e.Bounds.Width, e.Bounds.Top + 1);
                e.Graphics.DrawLine(pen, src, dst);
                pen.Dispose();
            }
            //Draw check mark
            if ((e.State & DrawItemState.Checked) == DrawItemState.Checked)
            {
                Size     arrSize = new Size(e.Bounds.Height - 4, e.Bounds.Height - 4);
                Bitmap   bitmap  = new Bitmap(arrSize.Height, arrSize.Height);
                Graphics g       = Graphics.FromImage(bitmap);
                g.Clear(Color.Transparent);
                ControlPaint.DrawMenuGlyph(g, new Rectangle(0, 1, arrSize.Width, arrSize.Height), MenuGlyph.Checkmark);
                bitmap.MakeTransparent();
                DrawMenyGlypth(e.Graphics, bitmap, new Rectangle(0, 0, arrSize.Width, arrSize.Height), new Rectangle(e.Bounds.Left + 2, // srcX
                                                                                                                     e.Bounds.Top + 1,  // srcY
                                                                                                                     arrSize.Width,
                                                                                                                     arrSize.Height), (e.State & DrawItemState.Selected) == DrawItemState.Selected);
            }
            //Draw arrow
            if (item.IsParent && withArrows)
            {
                Size     arrSize = new Size(e.Bounds.Height - 2, e.Bounds.Height - 2);
                Bitmap   bitmap  = new Bitmap(arrSize.Height, arrSize.Height);
                Graphics g       = Graphics.FromImage(bitmap);
                g.Clear(Color.Transparent);
                ControlPaint.DrawMenuGlyph(g, new Rectangle(0, 1, arrSize.Width, arrSize.Height), MenuGlyph.Arrow);
                bitmap.MakeTransparent();
                DrawMenyGlypth(e.Graphics, bitmap, new Rectangle(0, 0, arrSize.Width, arrSize.Height), new Rectangle(e.Bounds.Left + (e.Bounds.Width - e.Bounds.Height), // srcX
                                                                                                                     e.Bounds.Top + 1,                                   // srcY
                                                                                                                     arrSize.Width,
                                                                                                                     arrSize.Height), (e.State & DrawItemState.Selected) == DrawItemState.Selected);
            }

            brush.Dispose();
        }