【发布时间】:2011-11-25 02:36:18
【问题描述】:
我正在使用 Windows 窗体在 c# 中开发一个项目。 我和我所在的小组想要做到这一点,以便当用户将鼠标悬停在图像上时,在我们的例子中是一张卡片,该卡片的更大图像出现在鼠标箭头旁边,就像工具一样小费会起作用。 我不认为你可以使用工具提示来做到这一点我试过到处找, 任何建议或示例都会非常感谢您
【问题讨论】:
我正在使用 Windows 窗体在 c# 中开发一个项目。 我和我所在的小组想要做到这一点,以便当用户将鼠标悬停在图像上时,在我们的例子中是一张卡片,该卡片的更大图像出现在鼠标箭头旁边,就像工具一样小费会起作用。 我不认为你可以使用工具提示来做到这一点我试过到处找, 任何建议或示例都会非常感谢您
【问题讨论】:
你可能想看看这个Code Project Article
它向您展示了如何创建带有图像的OwnerDrawn ToolTip。
【讨论】:
感谢您的回复,我明白了一切。 我想做的是,当我将鼠标悬停在某个区域上时,该区域的不同图像会以与工具提示相同的方式弹出。所以经过一番研究,我想出了如何创建自己的工具提示类。
这里有一个例子。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
CustomToolTip tip = new CustomToolTip();
tip.SetToolTip(button1, "text");
tip.SetToolTip(button2, "writing");
button1.Tag = Properties.Resources.pelican; // pull image from the resources file
button2.Tag = Properties.Resources.pelican2;
}
}
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) // use this event to set the size of the tool tip
{
e.ToolTipSize = new Size(600, 1000);
}
private void OnDraw(object sender, DrawToolTipEventArgs e) // use this to customzie the tool tip
{
Graphics g = e.Graphics;
// to set the tag for each button or object
Control parent = e.AssociatedControl;
Image pelican = parent.Tag as Image;
//create your own custom brush to fill the background with the image
TextureBrush b = new TextureBrush(new Bitmap(pelican));// get the image from Tag
g.FillRectangle(b, e.Bounds);
b.Dispose();
}
}
}
【讨论】:
一种简单的方法是在指定位置隐藏/显示图片框。另一种方法是使用GDI API 加载和绘制(绘制)图像。
【讨论】: