繁体   English   中英

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

[英]display image on mouseover in windows form?

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

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

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

感谢您的反应,我得到了一切。 我想要做的是,当我在某个区域上进行鼠标移动时,该区域的不同图像将以与工具提示相同的方式弹出。 经过一些研究后,我想出了如何创建自己的工具提示类。

这是一个例子。

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加载和绘制(绘制)图像。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM