简体   繁体   中英

Prevent showing border of a disabled menu item on mouse hover

When I hover over a ToolStripMenuItem , it shows a border around it even when it is disabled. Is there any property to remove this?

I've looked for properties like Hover or Border but haven't found anything of use.

ToolStripProfessionalRenderer draws the border by default using MenuItemBorder property of ProfessionalColorTable .

To make the border just visible for enabled menu items, you can create your custom ProfessionalColorTable and your custom ToolStripProfessionalRenderer and add a new MenuItemEnabledBorder returning the default border color, and also overriding MenuItemBorder to return Color.Transparent .

Then in your custom renderer, it's enough to override OnRenderMenuItemBackground and draw the border using MenuItemEnabledBorder when the item is highlighetd.

Code

public class MyColorTable : ProfessionalColorTable
{
    public override Color MenuItemBorder { get { return Color.Transparent; } }
    public Color MenuItemEnabledBorder { get { return base.MenuItemBorder; } }
}

public class MyRenderer : ToolStripProfessionalRenderer
{
    public MyRenderer() : base(new MyColorTable()) { }
    protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e)
    {
        base.OnRenderMenuItemBackground(e);
        if (e.Item.Enabled && e.Item.Selected)
        {
            using (var pen = new Pen(((MyColorTable)ColorTable).MenuItemEnabledBorder))
            {
                var r = new Rectangle(2, 0, e.Item.Width - 4, e.Item.Height - 1);
                e.Graphics.DrawRectangle(pen, r);
            }
        }
    }
}

Then to use the renderer, it's enough to set it as renderer of your tool strip:

this.toolStrip1.Renderer = new MyRenderer();

在此处输入图片说明

You can override ToolStripProfessionalRenderer class and pass your colors (if you dont want border pass the original BackColor) to its base class constructor:

namespace WindowsFormsApplication11
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            menuStrip1.Renderer = new CustomColors();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }

    private class CustomColors : ToolStripProfessionalRenderer
    {
        public CustomColors() : base(new MyColors()) { }
    }

    private class MyColors : ProfessionalColorTable
    {
        public override Color MenuItemSelected
        {
            get { return Color.GreenYellow; }
        }
        public override Color MenuItemSelectedGradientBegin
        {
            get { return Color.DarkBlue; }
        }
        public override Color MenuItemSelectedGradientEnd
        {
            get { return Color.Yellow; }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM