本文介绍了是否有可能改变ToolStripMenuItem提示字体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个动态填充的ContextMenuStrip每个ToolStripMenuItem有一个格式化的文本提示。而且,为了让这个文本意义的用户,我必须使用等宽字体,如宋体。默认字体是有规律的,非等宽字体。我找不到任何的getter工具提示对象,也没有一种方法来覆盖其绘制的事件,也没有一种方法来设置它的风格。

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.

那么,它甚至可能改变ToolStripMenuItem的提示字体?

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

实施CustomToolTip继承自工具提示没有解决不了的问题,这是传递新的工具提示ToolStripMenuItem。

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

推荐答案

确定,这要归功于托尼·艾布拉姆斯威廉·安德鲁斯,该解决方案如下:

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的MouseEnter事件显示工具提示。

  • ToolStripMenuItem's MouseEnter event to show the tooltip.

    System.Windows.Forms.ToolStripMenuItem item = (sender as System.Windows.Forms.ToolStripMenuItem);
    toolTip.Show("ToolTipText", item.Owner);
    

  • ToolStripMenuItem的MouseLeave事件隐藏工具提示。

  • ToolStripMenuItem's MouseLeave event to hide the tooltip.

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

  • 这篇关于是否有可能改变ToolStripMenuItem提示字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    09-14 21:48