简体   繁体   English

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

[英]display image on mouseover in windows form?

I am working on a project in c# using windows forms. 我正在使用Windows窗体在c#中开发一个项目。 me and the group I am in want to make it so that when the user hovers their mouse over an image, in our case a card, that a larger image of that card appears next to the mouse arrow, much in the same way a tool tip would work. 我和我所在的小组想要这样做,以便当用户将鼠标悬停在图像上时(在我们的例子中是一张卡片),该卡片的较大图像会出现在鼠标箭头旁边,就像工具一样小费会奏效。 I don't think you can use a tool tip to do this i have tried looking everywhere, any advice or examples would be great thank you very much 我不认为你可以使用工具提示做到这一点我尝试到处寻找,任何建议或例子都会非常非常感谢你

You may want to look at this Code Project Article 您可能需要查看此代码项目文章

It shows you how to create an OwnerDrawn ToolTip with an Image. 它向您展示了如何使用Image创建OwnerDrawn工具提示。

Thanks for the responses I got everything figured out. 感谢您的反应,我得到了一切。 What I wanted to do was that when I moused over a certain area a different image for that area would popup in the same way that a tool tip did. 我想要做的是,当我在某个区域上进行鼠标移动时,该区域的不同图像将以与工具提示相同的方式弹出。 So after some research I figured out how to create my own tool tip class. 经过一些研究后,我想出了如何创建自己的工具提示类。

here's an example. 这是一个例子。

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();
    }
}

} }

A simple way to do is to hide/show a picture box at specified location. 一种简单的方法是隐藏/显示指定位置的图片框。 Another method is to load & draw (paint) an image using GDI API. 另一种方法是使用GDI API加载和绘制(绘制)图像。

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

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