问题描述
早安人士,我想在menuStrip上悬停时更改其菜单项的颜色.谁能帮我吗?
Good day people, I want to change the color of a menu item on my menuStrip when I hover above it. Can anyone help me?
推荐答案
您不能使用常规的MouseEnter
和MouseLeave
事件来执行此操作.您需要直接覆盖菜单渲染.您可以使用MenuStrip
类执行以下操作:
You can't do this using the normal MouseEnter
and MouseLeave
events. You need to directly override the menu rendering. You can do something like this, using the MenuStrip
class:
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; }
}
}
以防万一,您在使用MouseEnter
和MouseLeave
事件时会发生这种情况. (在MouseEnter
事件中,它使BackgroundColor
绿色,但是没有被调用):
And just in case you're interested, this is what happens when you use the MouseEnter
and MouseLeave
events. (Inside the MouseEnter
event, it was making the BackgroundColor
green, however that was not been called):
这篇关于C#如何更改menuStrip悬停颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!