如何使用图像创建自定义工具提示?

时间:2013-04-19 07:59:55

标签: c# custom-controls tooltip system.drawing

我想创建一个包含以下内容的客户工具提示:

  • 右上角的图片
  • 左侧的文字

目前我有一个继承自ToolTip Objekt的课程。

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 个答案:

答案 0 :(得分: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 :(得分: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); 
}