本文介绍了我如何使用.DrawImage()在工具提示上添加鼠标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我不确定是否可行,但是我尝试使用Graphics方法-DrawImage将工具提示动态添加到图像.鼠标悬停在图像上时,我看不到任何属性或事件,所以我不知道从哪里开始.我正在使用WinForms(在C#-.NET 3.5中).任何想法或建议,将不胜感激.谢谢.

Hey all, I am not sure if this is possible, but I am trying to dynamically add a tooltip to an image using the Graphics method - DrawImage. I dont see any properties or events for when the image is moused over or anything so I don't know where to begin. I am using WinForms (in C# - .NET 3.5). Any ideas or suggestions would be appreciated. Thanks.

推荐答案

我猜您有某种UserControl,并且您在OnPaint方法中调用了DrawImage().

I would guess that you have some sort of UserControl and you call DrawImage() in the OnPaint method.

鉴于此,您的工具提示将必须明确控制.基本上,在窗体上创建一个Tooltip,通过属性将其提供给控件,当控件收到MouseHover事件时显示工具提示,并在收到MouseLeave事件时隐藏工具提示.

Given that, your tooltip will have to controlled explicitly. Basically, create a Tooltip on your Form, give that to your control via a property, show the tooltip when your control received a MouseHover event and hide the tooltip when you receive a MouseLeave event.

类似这样的东西:

public partial class UserControl1 : UserControl
{
    public UserControl1() {
        InitializeComponent();
    }

    protected override void OnPaint(PaintEventArgs e) {
        base.OnPaint(e);

        // draw image here
    }

    public ToolTip ToolTip { get; set; }

    protected override void OnMouseLeave(EventArgs e) {
        base.OnMouseLeave(e);

        if (this.ToolTip != null)
            this.ToolTip.Hide(this);
    }

    protected override void OnMouseHover(EventArgs e) {
        base.OnMouseHover(e);

        if (this.ToolTip == null)
            return;

        Point pt = this.PointToClient(Cursor.Position);
        String msg = this.CalculateMsgAt(pt);
        if (String.IsNullOrEmpty(msg))
            return;

        pt.Y += 20;
        this.ToolTip.Show(msg, this, pt);
    }

    private string CalculateMsgAt(Point pt) {
        // Calculate the message that should be shown 
        // when the mouse is at thegiven point
        return "This is a tooltip";
    }
}

这篇关于我如何使用.DrawImage()在工具提示上添加鼠标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 12:36