Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to customize toolstrip button highlight color on mouse over

I use a ToolStrip in a C# winform application.
As I move the mouse over a button it gets highlighted (transparent blue color), I would like to change this color

I tried to use a custom renderer class

 toolStrip1.Renderer = new MyRenderer();
 ....
 class MyRenderer : ToolStripProfessionalRenderer
 {
 }

However, I don't know which method I should override to change that color.

like image 669
Ahmad Avatar asked Mar 20 '15 13:03

Ahmad


3 Answers

Actually, there is no built-in way (or at least I am not aware of it).

class MyRenderer : ToolStripProfessionalRenderer
{
    protected override void OnRenderButtonBackground(ToolStripItemRenderEventArgs e)
    {
        if (!e.Item.Selected)
        {
            base.OnRenderButtonBackground(e);
        }
        else
        {
            Rectangle rectangle = new Rectangle(0, 0, e.Item.Size.Width - 1, e.Item.Size.Height - 1);
            e.Graphics.FillRectangle(Brushes.Green, rectangle);
            e.Graphics.DrawRectangle(Pens.Olive, rectangle);
        }
    }
}
like image 196
Ivan Jovović Avatar answered Nov 15 '22 01:11

Ivan Jovović


You can do this by also making a custom implementation of ProfessionalColorTable and passing that to ToolStripProfessionalRenderer's constructor. You can then override some of the many properties in the colour table.

The ones that control the 'hover' effect are the ones with 'Selected' in their name, such as 'ButtonSelectedGradientBegin'.

You can discover the effect of the various properties by overriding them and returning bold, obvious colours, so you can easily see where they are used.

like image 43
Ashley Avatar answered Nov 15 '22 02:11

Ashley


Quick and Dirty

Use labels instead of buttons. You can set the an image, background image, and text of a toolstrip label. Then, set up your click events.

  • MouseEnter event for toolstrip item:

    private void tsi_MouseEnter(object sender, EventArgs e)
    {
        // Cast to allow reuse of method.
        ToolStripItem tsi = (ToolStripItem)sender;
    
        // Create semi-transparent picture.
        Bitmap bm = new Bitmap(tsi.Width, tsi.Height);
        for (int y = 0; y < tsi.Height; y++)
        {
            for (int x = 0; x < tsi.Width; x++)
                bm.SetPixel(x, y, Color.FromArgb(150, Color.White));
        }
    
        // Set background.
        tsi.BackgroundImage = bm;
    }
    
  • MouseLeave event:

    private void tsi_MouseLeave(object sender, EventArgs e)
    {
        (sender as ToolStripItem).BackgroundImage = null;
    }
    
like image 3
Tyler Pantuso Avatar answered Nov 15 '22 00:11

Tyler Pantuso