问题
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?
回答1:
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; }
}
}
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):
来源:https://stackoverflow.com/questions/37777688/c-sharp-how-to-change-menustrip-hover-color