Click here to Skip to main content
15,896,502 members
Articles / Programming Languages / C#
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
3.71/5 (5 votes)
13 Sep 2013CPOL 31.6K   2.9K   4   2
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

Image 1

To display an image, simply set the tag attributes :

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

The background color and forecolor can be modified:

C#
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:
    C#
    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:
    C#
    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:
    C#
    timer_fade.Interval = 50;
    timer_fade.Tick += timer_fade_Tick;
    
  4. In the timer_tick event, change the opacity of the form:
    C#
    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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
BugUnhandled Exception can occur! Pin
Member 1125783923-Nov-14 13:10
Member 1125783923-Nov-14 13:10 
If you move your mouse on and off the button or label in a circular motion really fast (that's what i did) the project crashes. I am very interested in making a custom form popup but with this error I would not be able to use your example as is. Also, I do not know to modify your example to utilize a custom Form.
GeneralMy vote of 2 Pin
herves16-Sep-13 0:33
herves16-Sep-13 0:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.