Tooltips显示图片

具体做法参见这篇文章,

还有相关的帮助链接.

需要注意的两点:

OwnerDraw属性设置为true;

IsBalloon属性不能设置为true;

我起初把IsBalloon设置为true,

结果图片怎么都显示不出来,

修改为False后,

图片顺利显示出来。

为防止链接失效,

把主要代码放在这里:

private const int Margin = 10;

// Set the tooltip's bounds.
private void tipButtons_Popup(object sender, PopupEventArgs e)
{
    int image_wid = 2 * Margin +
        Properties.Resources.happy.Width;
    int image_hgt = 2 * Margin +
        Properties.Resources.happy.Height;

    int wid = e.ToolTipSize.Width + 2 * Margin + image_wid;
    int hgt = e.ToolTipSize.Height;
    if (hgt < image_hgt) hgt = image_hgt;

    e.ToolTipSize = new Size(wid, hgt);
}
// Draw the tooltip.
private void tipButtons_Draw(object sender, DrawToolTipEventArgs e)
{
    // Draw the background and border.
    e.DrawBackground();
    e.DrawBorder();

    // Draw the image.
    e.Graphics.DrawImage(Properties.Resources.happy,
        Margin, Margin);

    // Draw the text.
    using (StringFormat sf = new StringFormat())
    {
        sf.Alignment = StringAlignment.Near;
        sf.LineAlignment = StringAlignment.Center;

        int image_wid = 2 * Margin +
            Properties.Resources.happy.Width;

        Rectangle rect = new Rectangle(image_wid, 0,
            e.Bounds.Width - image_wid, e.Bounds.Height);
        e.Graphics.DrawString(
            e.ToolTipText, e.Font, Brushes.Green, rect, sf);
    }
}

补充:

文本需要换行的话,

可以使用Environment.NewLine

例如:

this.toolTip1.SetToolTip(this.comboBox, "第一行文本," + Environment.NewLine + "第二行文本。");

行距如何控制,

尚未找到解决方法。

原文地址:https://www.cnblogs.com/myzw/p/14032777.html