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






3.71/5 (5 votes)
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:
- 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; } }
- 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; } }
- The
timer
is set like this:timer_fade.Interval = 50; timer_fade.Tick += timer_fade_Tick;
- In the
timer_tick
event, change the opacity of theform
: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(); } } }
- To show an
image
, set the background image of the form. - To show
text
, useCreateGraphics
andDrawstring
.
(Do not useDrawstring
before the opacity is1.0
.) - You can create a
form
on which you put whatever you like. Don't forget to set theFormBorderStyle
toNone
.
For more, please check the source.
This is my first time posting a tip here.