欢迎来到我的博客
Civil 3D开发与应用,欢迎加入QQ群:484124761
AutoCAD开发,欢迎加入QQ群:193522571

Tooltips显示图片

具体做法参见这篇文章,

还有相关的帮助链接.

需要注意的两点:

OwnerDraw属性设置为true;

IsBalloon属性不能设置为true;

我起初把IsBalloon设置为true,

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

修改为False后,

图片顺利显示出来。

为防止链接失效,

把主要代码放在这里:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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);
}

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// 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 + "第二行文本。");

行距如何控制,

尚未找到解决方法。

posted @   david96007  阅读(463)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示