Windows Develop Bookmark and Share   
 index > Windows Forms General > override Mouse hoover Color - highlight MenuStrip / ToolStripMenuItem
 

override Mouse hoover Color - highlight MenuStrip / ToolStripMenuItem

i would like to override the typical blue mousehoover color on theMenuStrip /ToolStripMenuItem and submembers.

witch 'Public override' do i need?
Natural_D  Thursday, September 24, 2009 7:49 PM
Check this thread.
Hans Passant.
  • Marked As Answer byNatural_D Saturday, September 26, 2009 2:59 PM
  •  
nobugz  Thursday, September 24, 2009 9:25 PM
Check this thread.
Hans Passant.
  • Marked As Answer byNatural_D Saturday, September 26, 2009 2:59 PM
  •  
nobugz  Thursday, September 24, 2009 9:25 PM
hi, thanks that was what i was looking for.
i took few days time, to test various scripting tecnhniques, before asking again.

when your menuStrip contain dropdown itemsi noticed that it is a sort of context menu behind the items?
can this be overriden?
or make a inherited class to add you costrum coded, so i can make it transparant?

i'm trying to make costum drawn shapes for ToolStripmenu items.
with round edges, but it looks awefull with the code from below.

than as told a few lines back, the childeren of the dropdown menu, look awefull.
i tried to detect it with the lines of code below and the visual studio designer, gets problems with the childeren, they aren't editable.


in resumen, if you try to draw custom shapes for tiem on a toolstrip menu, they look awefull, because there is still a box around them.


if (e.ToolStrip.IsDropDown)
{

}
else
{

}

if (e.Item.IsDropDown)
{

}
else
{

}


private static GraphicsPath CreateRoundRectangle(Rectangle rectangle, int radius)
{
GraphicsPath path = new GraphicsPath();
int l = rectangle.Left;
int t = rectangle.Top;
int w = rectangle.Width;
int h = rectangle.Height;
int d = radius << 1;
path.AddArc(l, t, d, d, 180, 90); // topleft
path.AddLine(l + radius, t, l + w - radius, t); // top
path.AddArc(l + w - d, t, d, d, 270, 90); // topright
path.AddLine(l + w, t + radius, l + w, t + h - radius); // right
path.AddArc(l + w - d, t + h - d, d, d, 0, 90); // bottomright
path.AddLine(l + w - radius, t + h, l + radius, t + h); // bottom
path.AddArc(l, t + h - d, d, d, 90, 90); // bottomleft
path.AddLine(l, t + h - radius, l, t + radius); // left
path.CloseFigure();
return path;
}
protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e)
{
base.OnRenderMenuItemBackground(e);
Size size = new Size();
size = e.Item.Size;
size.Height = size.Height;
size.Width = size.Width;
Rectangle rectangle = new Rectangle(Point.Empty, size);
if (!e.Item.Selected)
{
e.Graphics.FillRectangle(Brushes.LightGray, rectangle);
e.Graphics.DrawRectangle(Pens.Black, 1, 0, rectangle.Width - 2, rectangle.Height - 1);
using (GraphicsPath bw = CreateRoundRectangle(rectangle, 3))
{
using (Pen p = new Pen(Color.Black))
{
e.Graphics.DrawPath(p, bw);
}
}
}
else
{
e.Graphics.FillRectangle(Brushes.Gray, rectangle);
e.Graphics.DrawRectangle(Pens.Black, 1, 0, rectangle.Width - 2, rectangle.Height - 1);
using (GraphicsPath bw = CreateRoundRectangle(rectangle, 3))
{
using (Pen p = new Pen(Color.Black))
{
e.Graphics.DrawPath(p, bw);
}
}
}
Natural_D  Saturday, September 26, 2009 12:23 PM
No, you're stuck with what you call the "context menu". That's hard-coded, the drop-shadow effect would not work if you could tamper with it. Yeah, it doesn't look good. Your rounded rectangle is too big and you're not setting Graphics.SmoothingMode. Get rid of DrawRectangle. Making it look better than Microsoft's version isn't that easy.

Hans Passant.
nobugz  Saturday, September 26, 2009 2:58 PM
i tried various sizes for the round edges and various available rectangle formats properties fo the control.

i found another way to make it look good and easier to custom drawm, by using

ToolStripControlHost : button


this makes it good looking, but i'm stuck on the dropdown items.


thnx and kind regards



Natural_D  Saturday, September 26, 2009 3:04 PM
try this. this look pretty good.
accept i need to make a custom separator and costum dropdown/context


protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e)
{
base.OnRenderMenuItemBackground(e);
Size size = new Size();
size = e.Item.Size;
Rectangle defaultrectangle = new Rectangle(Point.Empty, size);
size.Height = size.Height - 4;
size.Width = size.Width - 4;
Rectangle rectangle = new Rectangle(Point.Empty, size);

if (!e.ToolStrip.IsDropDown)
{
if (!e.Item.Selected)
{
e.Graphics.FillRectangle(Brushes.Gray, defaultrectangle);
using (GraphicsPath bw = CreateRoundRectangle(rectangle, 3))
{
using (Pen p = new Pen(Color.Black))
{
e.Graphics.DrawPath(p, bw);
}
}
e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
}
else
{
e.Graphics.FillRectangle(Brushes.Gray, defaultrectangle);
using (GraphicsPath bw = CreateRoundRectangle(rectangle, 3))
{
using (Pen p = new Pen(Color.Black))
{
e.Graphics.DrawPath(p, bw);
}
}
e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
}
}
else
{
if (!e.Item.Selected)
{
e.Graphics.FillRectangle(Brushes.Gray, defaultrectangle);
}
else
{
e.Graphics.FillRectangle(Brushes.LightGray, defaultrectangle);
}
}
}
Natural_D  Saturday, September 26, 2009 3:32 PM
could you please check this work?

for some reason, if i add one of my own created items, disenger doesn't show them.
however the form1.designer.cs does contain all the added controls.
but doesn't display them when i compile and execute them.

i clean the code, this is the minumum needed

and 'base.DropDown = new MenuDropDown();' troubles again, once you add a cMenuStripItem, it did work briefly, i restarted the IDE. played a litle with code to clean it up and it stopped

: (



<pre lang="x-c#">using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms.Design;

namespace OwnerDraw_Test_3
{
    /*
   // try ing to achieve that my Costum control are standart item in    designer 
    public class cToolStripItemCollection : ToolStripItemCollection
    {
        cToolStripItemCollection()
        {
        
        }
    }
    */
    public class cMenuStrip : MenuStrip
    {
        public cMenuStrip()
        {
            this.BackColor = Color.Gray;
            this.Renderer = new MyRenderer();
            //this.Items = new cToolStripItemCollection();
        } 
    }

    #region cMyToolStripMenuItem
    /// <summary>
    /// 
    /// </summary>
    [ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.All)]
    public class cMyToolStripMenuItem : ToolStripMenuItem
    {
        //[DesignerSerializationVisibility
        //    (DesignerSerializationVisibility.Content)] 
        public bool rolloverValue = false;

        // This method defines the behavior of the MouseEnter event.
        // It sets the state of the rolloverValue field to true and
        // tells the control to repaint.
        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);
            this.rolloverValue = true;
            this.Invalidate();
        }

        // This method defines the behavior of the MouseLeave event.
        // It sets the state of the rolloverValue field to false and
        // tells the control to repaint.
        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            this.rolloverValue = false;
            this.Invalidate();
        }

        public cMyToolStripMenuItem()
        {
            base.DropDown = new MenuDropDown();
        }
    }

    #endregion

    #region Renderer
    /// <summary>
    /// MyRenderer
    /// </summary>
    class MyRenderer : ToolStripProfessionalRenderer
    {        
        private static GraphicsPath CreateRoundRectangle(Rectangle rectangle, int radius)
        {
            GraphicsPath path = new GraphicsPath();
            int l = rectangle.Left + 1;
            int t = rectangle.Top + 1;
            int w = rectangle.Width;
            int h = rectangle.Height;
            int d = radius << 1;
            path.AddArc(l, t, d, d, 180, 90); // topleft
            path.AddLine(l + radius, t, l + w - radius, t); // top
            path.AddArc(l + w - d, t, d, d, 270, 90); // topright
            path.AddLine(l + w, t + radius, l + w, t + h - radius); // right
            path.AddArc(l + w - d, t + h - d, d, d, 0, 90); // bottomright
            path.AddLine(l + w - radius, t + h, l + radius, t + h); // bottom
            path.AddArc(l, t + h - d, d, d, 90, 90); // bottomleft
            path.AddLine(l, t + h - radius, l, t + radius); // left
            path.CloseFigure();
            return path;
        }

        protected override void InitializeItem(ToolStripItem item)
        {
            base.InitializeItem(item);
            item.AutoSize = false;
        }

        protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e)
        {
            base.OnRenderMenuItemBackground(e);
            Size size = new Size();
            size = e.Item.Size;
            Rectangle defaultrectangle = new Rectangle(Point.Empty, size);
            size.Height = size.Height - 4;
            size.Width = size.Width - 4;
            Rectangle rectangle = new Rectangle(Point.Empty, size);

            if (!e.ToolStrip.IsDropDown)
            {
                e.Graphics.FillRectangle(Brushes.Gray, defaultrectangle);
                if (!e.Item.Selected)
                {
                    using (GraphicsPath bw = CreateRoundRectangle(rectangle, 3))
                    {
                        using (Pen p = new Pen(Color.Black))
                        {
                            e.Graphics.DrawPath(p, bw);
                        }
                    }
                    e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
                }
                else
                {
                    using (GraphicsPath bw = CreateRoundRectangle(rectangle, 3))
                    {
                        using (Pen p = new Pen(Color.Black))
                        {
                            e.Graphics.DrawPath(p, bw);
                        }
                    }
                    e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
                }
            }
            else
            {
                if (!e.Item.Selected)
                {
                    e.Graphics.FillRectangle(Brushes.Gray, defaultrectangle);
                }
                else
                {
                    e.Graphics.FillRectangle(Brushes.LightGray, defaultrectangle);
                }
            }
        }

        protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
        {
             base.OnRenderItemText(e);
             e.TextColor = Color.Cyan;
             if (!e.Item.Selected)
             {
                 e.Item.ForeColor = Color.Cyan;
             }
             else
             {
                 e.Item.ForeColor = Color.Blue;
             }
        }
    }

    #endregion


    #region ToolStripMenuButton

    [ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.All)]
    //Declare a class that inherits from ToolStripControlHost.
    public class ToolStripMenuButton : ToolStripControlHost
    {
        // Call the base constructor passing in a MonthCalendar instance.
        public ToolStripMenuButton() : base(new Button()) 
        {
            //base.dr= new MenuDropDown();
        }

        public Button ToolStripMenuButtonControl
        {
            get
            {
                return Control as Button;
            }
        }
    }

    #endregion

    #region Drop Down
    public class MenuDropDown : ToolStripDropDownMenu
    {
        protected override void OnItemClicked(ToolStripItemClickedEventArgs e)
        {
            // override click on Seperator
            if (e.ClickedItem is ToolStripSeparator)
            {

            }
            else
            {
                base.OnItemClicked(e);
            }
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            using (Pen p = new Pen(Color.Black))
            {
                e.Graphics.DrawRectangle(p,e.ClipRectangle);
            }
        }

    }
    #endregion
    [ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.All)]
    public class cToolStripSeparator : ToolStripSeparator
    {        
        protected override void  OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            using (Pen p = new Pen(Color.Gray))
            {
                e.Graphics.DrawRectangle(p, e.ClipRectangle);
            }
        }
    }
}
Natural_D  Saturday, September 26, 2009 5:23 PM

You can use google to search for other answers

Custom Search

More Threads

• WebBrowser Object and LoaderLock
• crystal report viewr
• MDI parent ToolStrip status handling
• custom scrollbars
• Printing Children
• How do I find out what ports are open on my computer
• more memory usage...
• help with BackGroundWorker
• Datatype Identifier...
• Issue with Modal Dialog..