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.
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);
}
}
}
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.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With