Hi!
This is very simple. To animate transitions you can use Timer component. Sample code:
public partial class Form1 : Form
{
private const double OPACITY_MIN = 0.5;
private const double OPACITY_MAX = 1.0;
private const double OPACITY_STEP = 0.05;
private const int TIMER_INTERVAL = 100;
private Timer _timer;
private bool _fading = false;
public Form1()
{
InitializeComponent();
_timer = new Timer();
_timer.Interval = TIMER_INTERVAL;
_timer.Tick += _timer_Tick;
}
private void _timer_Tick(object sender, EventArgs e)
{
if (_fading)
{
if (this.Opacity > OPACITY_MIN)
{
this.Opacity -= OPACITY_STEP;
}
else
{
this.Opacity = OPACITY_MIN;
_timer.Enabled = false;
}
}
else
{
if (this.Opacity < OPACITY_MAX)
{
this.Opacity += OPACITY_STEP;
}
else
{
this.Opacity = OPACITY_MAX;
_timer.Enabled = false;
}
}
}
private void Form1_MouseLeave(object sender, EventArgs e)
{
_fading = true;
_timer.Enabled = true;
}
private void Form1_MouseEnter(object sender, EventArgs e)
{
_fading = false;
_timer.Enabled = true;
}
}
You can handle other Form's events in the same way. But you should remember that if you place i.e. Panel control and set Dock property to Dock.Fill this code will not work. You will have to handle Panel's events (Enter and Leave).
http://msdn.microsoft.com/en-us/library/system.windows.forms.timer%28v=vs.110%29.aspx[
^]
[Update for Bill Woodruff's comment]
MouseHover event isn't fired immediatelly when mouse enters the form. According to MSDN documentation:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mousehover%28v=vs.110%29.aspx[
^]
there is some amount of time before that event is fired:
http://msdn.microsoft.com/en-us/library/system.windows.forms.systeminformation.mousehovertime%28v=vs.110%29.aspx[
^]
and specified area within which the mouse pointer has to stay before event is fired:
http://msdn.microsoft.com/en-us/library/system.windows.forms.systeminformation.mousehoversize%28v=vs.110%29.aspx[
^]
So, using MouseEnter and MouseLeave is first choice. The second choice should be Form's Activated and Deactivated events which are fired when you i.e. switch to/from another window/app.
-- End update --
[Update - For MasterCodeon's comment]
I admit that didn't test this solution with controls placed on form. In that situation Form's MouseEnter and MouseLeave will be fired even when mouse cursor will enter/leave controls located on form.
So, I have found workaround to this problem entirely based on Timer. Basically I got rid of MouseEnter and MouseLeave event handlers and added checking (for some interval) that cursor's position is within Form's bounds.
Additionally I've added an option (SMOOTH_SHOWING) for setting behaviour how form will appear when cursor enters the form. If set to yes, then form will be shown smoothly (like in fading), if no then form will appear immediatelly. Below is my code:
public partial class Form1 : Form
{
private const double OPACITY_MIN = 0.5;
private const double OPACITY_MAX = 1.00;
private const double OPACITY_STEP = 0.05;
private const bool SMOOTH_SHOWING = true;
private const int TIMER_INTERVAL = 100;
private Timer _timer;
public Form1()
{
InitializeComponent();
_timer = new Timer();
_timer.Interval = TIMER_INTERVAL;
_timer.Tick += _timer_Tick;
_timer.Enabled = true;
}
void _timer_Tick(object sender, EventArgs e)
{
var inside = this.Bounds.Contains(Cursor.Position);
if (inside)
{
if (this.Opacity < OPACITY_MAX)
{
if (SMOOTH_SHOWING)
{
this.Opacity += OPACITY_STEP;
}
else
{
this.Opacity = OPACITY_MAX;
}
}
}
else
{
if (this.Opacity > OPACITY_MIN)
{
this.Opacity -= OPACITY_STEP;
}
}
}
}
-- End update ---
I hope it help you.