Click here to Skip to main content
Click here to Skip to main content

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

, 13 Sep 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Sean Chung
Software Developer
China China
No Biography provided

Comments and Discussions

 
BugUnhandled Exception can occur! PinmemberMember 1125783923-Nov-14 14:10 
GeneralMy vote of 2 Pinmemberherves16-Sep-13 1:33 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150302.1 | Last Updated 13 Sep 2013
Article Copyright 2013 by Sean Chung
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid