C# how to change menuStrip hover color? [duplicate]

﹥>﹥吖頭↗ 提交于 2020-03-06 03:02:55

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!