Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Custom Popup Fade-in Window in C#/Winform/.NET

0.00/5 (No votes)
13 Sep 2013 1  
In this tip, I'll show you how I make a replacement for tooltip

Introduction

Sometimes, we come across the situation where we need to put a button, a picture, or a listview on a tooltip.

Building a form is how I make it, rather than use tooltip.

Using the Code

To display an image, simply set the tag attributes :

button1.Tag = Image.FromFile("img.jpg");
PopUpWindow.ClassPopUp pop = new PopUpWindow.ClassPopUp();
pop.Initialize(button1, true, "This Is A Test!");

The background color and forecolor can be modified:

pop2.Initialize(label1, false, "This is another test!", 
new System.Drawing.Size(320, 240), Color.Black, Color.White);    

The fade-in/out effect is very simple:

  1. Override the OnLoad event of the form:
    protected override void OnLoad(EventArgs e)
            {
                base.OnLoad(e);
                this.Location = MousePosition;
                if (!DesignMode)
                {
                    fadeIn = true;
                    Opacity = 0;
                    timer_fade.Enabled = true;
                }
            } 
  2. Override the OnClosing event of the form:
     protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
            {
                base.OnClosing(e);
                base.OnClosing(e);
                if (e.Cancel)
                    return;
                if (Opacity > 0)
                {
                    fadeIn = false;
                    timer_fade.Enabled = true;
                    e.Cancel = true;
                }
            } 
  3. The timer is set like this:
     timer_fade.Interval = 50;
     timer_fade.Tick += timer_fade_Tick; 
  4. In the timer_tick event, change the opacity of the form:
    void timer_fade_Tick(object sender, EventArgs e)
            {
                if (fadeIn)
                {
                    Opacity += (timer_fade.Interval / 166.0);
                    timer_fade.Enabled = (Opacity < 1.0);
                    fadeIn = (Opacity < 1.0);
                }
                else
                {
                    Opacity -= (timer_fade.Interval / 166.0);
                    if (this.Opacity > 0)
                    {
                        timer_fade.Enabled = true;
                    }
                    else
                    {
                        timer_fade.Enabled = false;
                        Close();
                    }
                }
            }  
  5. To show an image, set the background image of the form.
  6. To show text, use CreateGraphics and Drawstring.
    (Do not use Drawstring before the opacity is 1.0.)
  7. You can create a form on which you put whatever you like. Don't forget to set the FormBorderStyle to None.

For more, please check the source.

This is my first time posting a tip here.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here