4

I've got a menu with a custom Renderer:

menuMain.Renderer = new ToolStripProfessionalRenderer(new MenuColors());

Is there a way to change the font or perhaps make the menu item italic when moving the mouse over it?

I've got the event to change the background, but don't know about font / font color?

internal class MenuColors : ProfessionalColorTable
{
    public override Color MenuItemSelected
    {
        get { return MenuHoverColor; }
    }
}
Cameron Castillo
  • 2,712
  • 10
  • 47
  • 77

2 Answers2

5

You can inherit from ToolStripProfessionalRenderer and override OnRenderItemText and use ToolStripItemTextRenderEventArgs like below:

public class SampleRenderer : ToolStripProfessionalRenderer
{
    protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
    {
        // Here set e.TextFont, e.TextColor and so on, For example:
        if (e.Item.Selected)
        {
            e.TextColor = Color.Blue;
            e.TextFont = new Font(e.Item.Font, FontStyle.Italic | FontStyle.Bold);
        }
        base.OnRenderItemText(e);
    }
}

You can use e.Item properties to decide what to do in different situations, for example, if you want that logic only works on sub menus you can use a code like this:

if (e.Item.Selected && e.Item.OwnerItem != null)
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • Thanks - that is what I'm looking for. How do you separate the logic for main menu's and sub menus (dropdown items). This code changes both. – Cameron Castillo Sep 01 '15 at 10:59
4

You can change ForeColor and Font Property manually using properties of ToolStrip and Renderer will use them when rendering.

And if you want different Font and Color when mouse enter or when an item is selected, you should handle suitable events to change the font and color in that siuations, for example the code below changes the font of item to italic, when the mouse is over the item:

private void toolStripMenuItem_MouseEnter(object sender, EventArgs e)
{
    var item=(ToolStripMenuItem)sender;
    item.ForeColor = Color.Blue;
    item.Font = new Font(item.Font, FontStyle.Italic | FontStyle.Bold );
}

private void toolStripMenuItem_MouseLeave(object sender, EventArgs e)
{
    var item = (ToolStripMenuItem)sender;
    item.ForeColor = Color.Green;
    item.Font = new Font(item.Font, FontStyle.Regular);
}

You can assign these handlers to your ToolStripMenuItems dynamically like this:

YourToolStripMenuItem.MouseEnter += new System.EventHandler(this.toolStripMenuItem_MouseEnter);
YourToolStripMenuItem.MouseLeave += new System.EventHandler(this.toolStripMenuItem_MouseLeave);

Here is a screen shot of a custom renderer that I use for toolstrip with Office 2003 Styles while I have changed Font and color of ToolStripButtons and ToolStripMenuItems and then when the mouse is over an item I change its font style to bold and italic and its forecolor to green. enter image description here

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • Thanks - but I need to do with code as my menu items are created dynamically with code. – Cameron Castillo Aug 31 '15 at 14:27
  • Thanks for the edit. I am actually currently using the mouse enter and leave events, but I am specifically looking to do it via a build in renderer like ToolStripProfessionalRenderer or ProfessionalColorTable as it gives more flexibility and options. But your code does help for an option b. – Cameron Castillo Aug 31 '15 at 14:45
  • Could be useful for WPF projects `ToolStripMenuItem1.Font = new Font(ToolStripMenuItem1.Font, System.Drawing.FontStyle.Bold);` – vinsa Nov 02 '17 at 23:06
  • @vinsa For WPF, you may find [this post](https://stackoverflow.com/a/46268362/3110834) useful. – Reza Aghaei Nov 03 '17 at 02:21