Change WinForms button highlight color
Asked Answered
F

2

9

I found this page, which outlines how to change the rendering for a MenuStrip and its items.

I want to use this, but the problem is that the highlight color when you hover over a button doesn't match it.

Is there any way to change the highlight color from blue to yellow? I've tried using the MouseHover and MouseLeave events, but for some reason they're really slow, and they change the button to a solid color, which looks bad, but leaves a border on the edge of the button that doesn't change.

In the designer:

this.ButtonName.MouseHover += new System.EventHandler(button_mousehover);

And then in the Code:

private void button_mousehover(object sender, EventArgs e)
{
    Button btn = sender as Button;
    btn.BackColor = Color.Yellow;
}

Is there anything as easy as in the link I posted above to change the highlight color from blue to something else?

Here's the code for changing the rendering of the menu strip:

private void myForm Load(object sender, EventArgs e)
{
    myMenuStrip.Renderer = new MenuRenderer();
{

private class MenuRenderer : ToolStripProfessionalRenderer
{
    public MenuRenderer() : base(new MyColors()) { }
}

private class MyColors : ProfessionalColorTable
{
    public override Color MenuItemSelectedGradientBegin
    {
        get { return Color.Orange; }
    }
    public override Color MenuItemSelectedGradientEnd
    {
        get { return Color.Yellow; }
    }
    public override Color MenuItemPressedGradientBegin
    {
        get{ return Color.Yellow; }
    }
    public override Color MenuItemPressedGradientEnd
    {
        get { return Color.Orange; }
    }
    public override Color MenuItemSelected
    {
        get { return Color.Gold; }
    }
}

So it'll change the background of a hovered-over menu item to an orange-yellow gradient, change it to a yellow-orange gradient on click, and any item in the menu will have a gold highlight on hovering.

What I'm trying to do is do that last part (change the highlight to gold/yellow) for the buttons in my form.

Fallow answered 7/1, 2015 at 19:1 Comment(6)
Can you post your code for changing the rendering of a MenuStrip item?Uptotheminute
Sure, just one moment, I'll edit it into the comment.Fallow
wait, what colors you don't see that you expect to see?Uptotheminute
I can see the highlight color as you described. When I hover over the MenuItems I can see them gold.Uptotheminute
Yeah the menu strip highlighting works fine. What I want to do is have the same degree of control over a Button (gradients, etc.)Fallow
Have you tried FlatAppearance property?Cinnamon
G
16

In the properties of the button:

under FlatStyle, select Flat.

Then, expand FlatAppearance and under MouseOverBackColor, select the highlight color you want. Alternatively, you can also enter the RGB color you want, under the MouseOverBackColor.

Gel answered 30/1, 2018 at 8:55 Comment(1)
Quick and simple. One of those underrated and easily overlooked answers on SO.Bubaline
C
1

You can take a look at the Button Renderer.

Why do you want to override the renderer when you can simply subscribe to the MouseHover event like so:

this.someButtonName.MouseHover += (s,e) => 
{
   this.someButtonName.BackColor = Color.Yellow;
};

I recommend you use a mouse leave too in order to reset the button to it's initial color when your mouse isn't on it anymore.

Certificate answered 7/1, 2015 at 19:3 Comment(5)
Yeah, I did do this, but it was A) Slow for some reason (took about half a second to change the BackColor) and B) fills the button solid except for the border, which doesn't look very good.Fallow
I agree it doesn't give the best results. Have you tried changing other properties such as the border colorsCertificate
Okay, interesting, I went looking through the properties and found that there's a collection of properties under FlatAppearance, one of which is MouseOverBackColor which does change the background fairly immediately (albeit still a solid color), but only works if you change the FlatStyle to Flat or Popup.Fallow
Actually, with some tweaking, it looks pretty nice. If nobody knows how to change the highlight color I'll probably end up using this.Fallow
Great. Im sure there a tons of example online you can find where others successfully overrided the button highlight.Certificate

© 2022 - 2024 — McMap. All rights reserved.