Zyzzyx
Zyzzyx

Reputation: 153

display image on mouseover in windows form?

I am working on a project in c# using windows forms. 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

Upvotes: 8

Views: 14086

Answers (3)

Zyzzyx
Zyzzyx

Reputation: 153

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

}

Upvotes: 7

Mark Hall
Mark Hall

Reputation: 54562

You may want to look at this Code Project Article

It shows you how to create an OwnerDrawn ToolTip with an Image.

Upvotes: 8

KV Prajapati
KV Prajapati

Reputation: 94653

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.

Upvotes: 2

Related Questions