如何创建带有图像的自定义工具提示?

3

我希望创建一个包含以下内容的自定义工具提示:

  • 图片位于右上角
  • 文本位于左侧

目前我有一个继承自ToolTip对象的类。

class CustomToolTip : ToolTip
{
    public CustomToolTip()
    {
        this.OwnerDraw = true;
        this.Popup += new PopupEventHandler(this.OnPopup);
        this.Draw += new DrawToolTipEventHandler(this.OnDraw);
    }

    private void OnPopup(object sender, PopupEventArgs e)
    {
        e.ToolTipSize = new Size(200, 100);
    }

    private void OnDraw(object sender, DrawToolTipEventArgs e)
    {

    }
}

但我不知道在"OnDraw-Event"中怎么做才能展示一张带文字的图片。

谢谢你的帮助。

2个回答

2

请查看这个http://www.codeproject.com/Articles/42050/ToolTip-With-Image-C链接,应该已经讲解得足够清楚了。

myImageRectangle = Rectangle.Inflate(myToolTipRectangle, -BORDER_THICKNESS, -BORDER_THICKNESS);
Image toolTipImage = Image.FromFile(filepath);        
if (toolTipImage != null)
    {
        myImageRectangle.Width = 200;
        myTextRectangle = new Rectangle(myImageRectangle.Right, myImageRectangle.Top, (myToolTipRectangle.Width - myImageRectangle.Right), myImageRectangle.Height);
        myTextRectangle.Location = new Point(myImageRectangle.Right, myImageRectangle.Top);
        e.Graphics.FillRectangle(myBackColorBrush, myTextRectangle);
        e.Graphics.DrawImage(toolTipImage, myImageRectangle);
        e.Graphics.DrawString(e.ToolTipText, myFont, 
        myTextBrush, myTextRectangle, myTextFormat);
    }

1
非常感谢您的回答,但请不要发布“仅链接”答案。链接可能在未来过时,使您的答案变得无用。考虑通过添加链接文章的要点、如果不太长则发布一些示例代码来编辑您的答案。 - John Willemse

1
请尝试使用GDI+
protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);
    Image img = Image.FromFile("C:\filepath\filename.jpg");
    e.Graphics.DrawImage(img, 0, 0);
    var YourTipTextPoint = new Point(0,0);
    e.Graphics.DrawString("Hello World", SystemFonts.DefaultFont, Brushes.Black, YourTipTextPoint); 
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接