【问题标题】:How to create a custom ToolTip with an image?如何使用图像创建自定义工具提示?
【发布时间】:2013-04-19 07:59:54
【问题描述】:

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

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

目前我有一个继承自 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”中显示带有文本的图像。

感谢您的帮助

【问题讨论】:

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


    【解决方案1】:

    看看这个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);
        }
    

    【讨论】:

    • 感谢您的回答,请不要发布“仅链接”的答案。该链接将来可能会过时,从而使您的答案毫无用处。考虑通过添加链接文章的要点来编辑您的答案,如果不太长,请发布一些示例代码。
    【解决方案2】:

    请尝试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); 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-02
      • 2018-09-16
      • 1970-01-01
      • 2019-10-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多