Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to change ToolStripMenuItem tooltip font?

I have a dynamically filled ContextMenuStrip where each ToolStripMenuItem has a formatted text for the tooltip. And, in order for this text to make sense to the user, I must use a monospaced font, such as "Courier New". The default font is a regular, non Monospaced font. I couldn't find any getter for the ToolTip object nor a way to override its Draw event nor a way to set its style.

So, is it even possible to change ToolStripMenuItem's tooltip font?

Implementing CustomToolTip that inherits from ToolTip doesn't solve the issue, which is passing the new tooltip to ToolStripMenuItem.

like image 964
ElyaSh Avatar asked Jan 11 '11 12:01

ElyaSh


1 Answers

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

  • A static instance of ToolTip which 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's Popup 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's Draw 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's MouseEnter event to show the tooltip.

    System.Windows.Forms.ToolStripMenuItem item = (sender as System.Windows.Forms.ToolStripMenuItem);
    toolTip.SetToolTip(item.Owner, "ToolTipText");
    
like image 190
ElyaSh Avatar answered Nov 02 '22 04:11

ElyaSh