简体   繁体   English

当光标离开激活的项目时,工具条菜单项会自动更改其颜色

[英]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. 我已经创建了Windows窗体工具,并添加了一个包含几个项目的菜单条和一个下拉菜单。 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. 请帮助我,当鼠标光标离开该元素时,将itemcolor保持为蓝色/中等蓝色。

Cheers 干杯

Florian 弗洛里安

Found the solution by trying several renderer options out: I deleted my former "BlueRenderer" and have specified a ProfessionalColorTable instead: 通过尝试一些渲染器选项找到了解决方案:我删除了以前的“ BlueRenderer”,而是指定了ProfessionalColorTable:

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: 然后,按如下方式初始化ToolStripProfessionalRenderer:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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