Search code examples
c#winformstoolstripmenu

Can you change font for ToolStripMenuItem in custom Renderer


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

Solution

  • 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)