大家好,我不确定这是否可行,但我正在尝试使用 Graphics 方法 - DrawImage 为图像动态添加工具提示。当图像被鼠标悬停或任何东西时,我看不到任何属性或事件,所以我不知道从哪里开始。我正在使用 WinForms(在 C# - .NET 3.5 中)。任何想法或建议将不胜感激。谢谢。
问问题
14086 次
2 回答
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";
}
}
于 2010-11-28T05:48:45.803 回答
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
}
}
于 2010-11-28T07:43:14.697 回答