在Windows窗体中以鼠标悬停显示图像?

Zyz*_*zyx 6 c# winforms

我正在使用Windows窗体在c#中开发一个项目.我和我所在的小组想要这样做,以便当用户将鼠标悬停在图像上时(在我们的例子中是一张卡片),该卡片的较大图像会出现在鼠标箭头旁边,就像工具一样小费会奏效.我不认为你可以使用工具提示做到这一点我尝试到处寻找,任何建议或例子都会非常非常感谢你

Mar*_*all 7

您可能需要查看此代码项目文章

它向您展示了如何使用Image 创建OwnerDrawn工具提示.


Zyz*_*zyx 5

感谢您的答复,我已经弄明白了。我想做的是,当我将鼠标悬停在某个区域上时,该区域的另一张图像将以与工具提示相同的方式弹出。因此,在进行了一些研究之后,我想出了如何创建自己的工具提示类的方法。

这是一个例子。

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

}