9

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.

3 Answers 3

14

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);
        }
    }
}
3

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;
    }
    
2

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.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.