2016-06-12 146 views
-2

美好的一天人们,我想改变菜单项上的菜单项的颜色,当我把它悬停在它上面时。谁能帮我?C#如何更改menuStrip悬停颜色?

enter image description here

+0

不,它不是。我想改变主要项目悬停颜色,并且在那个问题上没有关于它的文字颜色 –

+0

? – Shaharyar

+0

不行,文字背景 –

回答

1

您无法使用正常MouseEnterMouseLeave事件做到这一点。您需要直接覆盖菜单渲染。你可以做这样的事情,使用MenuStrip类:

private class renderer : ToolStripProfessionalRenderer { 
    public renderer() : base(new cols()) {} 
} 

private class cols : ProfessionalColorTable { 
    public override Color MenuItemSelected { 
     // when the menu is selected 
     get { return Color.Blue; } 
    } 
    public override Color MenuItemSelectedGradientBegin { 
     get { return Color.Black; } 
    } 
    public override Color MenuItemSelectedGradientEnd { 
     get { return Color.White; } 
    } 
} 

以防万一你有兴趣,这是当你使用MouseEnterMouseLeave事件会发生什么。 (里面的MouseEnter事件,它是使BackgroundColor绿色的,但是这并没有被调用):

Only the leave event was been called.

+0

正是我一直在寻找的,谢谢你,先生。 –

+0

很高兴能帮到你。注意你可以用'ProfessionalColorTable'类做更多的事情。请参阅此MSDN文章:https://msdn.microsoft.com/en-us/library/System.Windows.Forms.ProfessionalColorTable(v=vs.110).aspx – carefulnow1

+0

“Enter”和“Leave”方法在哪里在说什么?为什么背景应该是绿色的? – Shaharyar