2010-11-28 89 views
1

将工具提示上的鼠标移到图像上嘿,我不知道这是否可能,但我正在尝试使用Graphics方法 - DrawImage动态地添加工具提示到图像。我没有看到任何属性或事件的时候,图像被蒙上了阴影或任何东西,所以我不知道从哪里开始。我正在使用WinForms(在C# - .NET 3.5中)。任何想法或建议,将不胜感激。谢谢。如何使用.DrawImage()

回答

1

我想你有某种UserControl,你在OnPaint方法中调用DrawImage()

鉴于此,您的工具提示必须明确控制。基本上,在你的表单上创建一个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"; 
    } 
} 
+0

谢谢,明天我会试试,并报告回来。 – Travyguy9 2010-11-28 06:17:58

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 
     } 
    }