简体   繁体   中英

Tool strip menu item changes its color by itself when cursor leaves activated item

I have created a Windows Forms Tool and added a menu strip with several items and a dropdown menu. It all works fine and when I select the dropdown menu item "File" it expands correctly. But when the cursor leaves the tool strip menu item "File" to the dropdown menu items, it changes its color automatically to white so that you cannot read the white text anymore:

Unclicked menu strip

Clicked menu strip with mouse cursor on dropdown items

I also had similar issues with color changes of the menu strip before so that I already defined a Renderer:

class BlueRenderer : ToolStripProfessionalRenderer
{
    protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e)
    {
        if (!e.Item.Selected)
        {
            base.OnRenderMenuItemBackground(e);
            e.Item.BackColor = Color.MediumBlue;  

        }
        else
        {                
            Rectangle rc = new Rectangle(Point.Empty, e.Item.Size);
            e.Graphics.FillRectangle(Brushes.Blue, rc);                
            e.Graphics.DrawRectangle(Pens.MediumBlue, 1, 0, rc.Width - 2, rc.Height - 1);                
            e.Item.BackColor = Color.MediumBlue;
            base.OnRenderItemBackground(e);
            e.Item.BackColor = Color.MediumBlue;
        }
    }
    protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
    {
        base.OnRenderItemText(e);
        if (!e.Item.Selected)
        {
            e.Item.ForeColor = SystemColors.ControlLightLight;
        }
        else
        {
            e.Item.ForeColor = SystemColors.ControlLightLight;
        }
    }
}

I think I have to change another property or behavior in renderer, but I don't even know which one and how to change it. Please help me to simply keep the itemcolor in blue / medium blue when mouse cursor leaves this element.

Cheers

Florian

Found the solution by trying several renderer options out: I deleted my former "BlueRenderer" and have specified a ProfessionalColorTable instead:

public class ownColorTable : System.Windows.Forms.ProfessionalColorTable
{
    public override Color MenuItemPressedGradientBegin
    {
        get
        {
            return Color.Blue;
        }
    }

    public override Color MenuItemPressedGradientEnd
    {
        get
        {
            return Color.Blue;
        }
    }

Then I initalized the ToolStripProfessionalRenderer as follows:

menuStrip1.Renderer = new ToolStripProfessionalRenderer(new ownColorTable());

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