Can you change font for ToolStripMenuItem in custom Renderer
Asked Answered
O

2

5

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; }
    }
}
Orthopteran answered 31/8, 2015 at 11:57 Comment(0)
A
6

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)
Adynamia answered 31/8, 2015 at 14:43 Comment(2)
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.Orthopteran
I'm doing something about this but I have an issue. I want to change the text color based on some form's current value, like, based on a variable, a control's color or something, but I can't do that, Like, if current skin is dark, I want white text, else I want black text. it seems inside this class there's no concept of anything that's in the form. This class is declared as a private class "name" : ToolStripProfessionalRenderer INSIDE a public partial class Form1 : Form. I don't know if that can make any difference..Lenwood
A
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

Adynamia answered 31/8, 2015 at 13:49 Comment(4)
Thanks - but I need to do with code as my menu items are created dynamically with code.Orthopteran
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.Orthopteran
Could be useful for WPF projects ToolStripMenuItem1.Font = new Font(ToolStripMenuItem1.Font, System.Drawing.FontStyle.Bold);Leesa
@Leesa For WPF, you may find this post useful.Adynamia

© 2022 - 2024 — McMap. All rights reserved.