嘿所有,我不确定这是否可行,但我试图使用Graphics方法 - DrawImage动态地向图像添加工具提示。我没有看到任何属性或事件,当图像被鼠标悬停或任何东西,所以我不知道从哪里开始。我正在使用WinForms(在C# - .NET 3.5中)。任何想法或建议将不胜感激。感谢。
答案 0 :(得分:1)
我猜您有某种UserControl
,并且您在DrawImage()
方法中致电OnPaint
。
鉴于此,您的工具提示必须明确控制。基本上,在您的表单上创建Tooltip
,通过属性将其提供给您的控件,在您的控件收到MouseHover
事件时显示工具提示,并在收到MouseLeave
事件时隐藏工具提示
这样的事情:
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";
}
}
答案 1 :(得分:1)
请记住,您必须存储您正在绘制的图像的边界
并在mouseMove event
检查该区域current Mouse cursor
的位置,然后显示工具提示,否则将其隐藏。
ToolTip t;
private void Form1_Load(object sender, EventArgs e)
{
t = new ToolTip(); //tooltip to control on which you are drawing your Image
}
Rectangle rect; //to store the bounds of your Image
private void Panel1_Paint(object sender, PaintEventArgs e)
{
rect =new Rectangle(50,50,200,200); // setting bounds to rect to draw image
e.Graphics.DrawImage(yourImage,rect); //draw your Image
}
private void Panel1_MouseMove(object sender, MouseEventArgs e)
{
if (rect.Contains(e.Location)) //checking cursor Location if inside the rect
{
t.SetToolTip(Panel1, "Hello");//setting tooltip to Panel1
}
else
{
t.Hide(Panel1); //hiding tooltip if the cursor outside the rect
}
}