Can I change the font of ToolStripMenuItem tooltip?

I have a dynamically populated ContextMenuStrip where each ToolStripMenuItem has formatted text for a tooltip. And for this text to make sense to the user, I have to use a monospace font, such as "Courier New". The default font is a regular font without a monospaced font. I could not find any getter for the ToolTip object, as well as a way to override its Draw event and a way to set its style.

So, is it possible to change the font of ToolStripMenuItem tooltip at all?

The implementation of CustomToolTip, which is inherited from ToolTip, does not solve the problem that passes the new ToolStripMenuItem tooltip.

+5
source share
3 answers

OK, thanks to Tony Abrams and William Andrus , the solution is as follows:

  • The static instance of the ToolTip that was initialized.

    toolTip = new ToolTip();
    toolTip.OwnerDraw = true;
    toolTip.Draw += new DrawToolTipEventHandler(tooltip_Draw);
    toolTip.Popup += new PopupEventHandler(tooltip_Popup);    
    toolTip.UseAnimation = true;
    toolTip.AutoPopDelay = 500;
    toolTip.AutomaticDelay = 500;
    
  • Tooltip event to set its size.

    void tooltip_Popup(object sender, PopupEventArgs e)
    {
        e.ToolTipSize = TextRenderer.MeasureText(toolTipText, new Font("Courier New", 10.0f, FontStyle.Bold));
        e.ToolTipSize = new Size(e.ToolTipSize.Width + TOOLTIP_XOFFSET, e.ToolTipSize.Height + TOOLTIP_YOFFSET);
    }
    
  • ToolTip Draw an event for actual drawing.

    void tooltip_Draw(object sender, DrawToolTipEventArgs e)
    {
    Rectangle bounds = e.Bounds;
    bounds.Offset(TOOLTIP_XOFFSET, TOOLTIP_YOFFSET);
    DrawToolTipEventArgs newArgs = new DrawToolTipEventArgs(e.Graphics, e.AssociatedWindow, e.AssociatedControl, bounds, e.ToolTipText, toolTip.BackColor, toolTip.ForeColor, new Font("Courier New", 10.0f, FontStyle.Bold));
        newArgs.DrawBackground();
        newArgs.DrawBorder();
        newArgs.DrawText(TextFormatFlags.TextBoxControl);
    }
    
  • ToolStripMenuItem MouseEnter event to display a tooltip.

    System.Windows.Forms.ToolStripMenuItem item = (sender as System.Windows.Forms.ToolStripMenuItem);
    toolTip.Show("ToolTipText", item.Owner);
    
  • ToolStripMenuItem MouseLeave event to hide the tooltip.

    System.Windows.Forms.ToolStripMenuItem item = (sender as System.Windows.Forms.ToolStripMenuItem);
    toolTip.Hide(item.Owner);
    
+10
source

You can create your own ToolTip class (CustomToolTip), which inherits from ToolTip. Then you have to handle the OnDraw event. Inside this event you can change the font.

Look here for an example (there is an example of vb and C #).

EDIT

( IE: OnMouseOver, OnMouseLeave toolstripmenuitem). , customtoolstripmenuitem, , , toolstripmenuitem propety/object.

+2

, , , ToolTip, . Draw, .

public void SetToolTipInstance(ToolStrip ts, ToolTip tt)
{
    Type type = ts.GetType.BaseType;
    int propToolTip = Convert.ToInt32(type.GetField("PropToolTip", BindingFlags.NonPublic | BindingFlags.Static).GetValue(ts));
    dynamic ps = type.BaseType.GetProperty("Properties", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(ts);
    ps.GetType.GetMethod("SetObject", BindingFlags.Instance | BindingFlags.Public).Invoke(ps, {propToolTip,tt});
}
0

All Articles