2020-03-17 02:45:19 +13:00
|
|
|
|
using System;
|
2019-07-05 05:04:14 +12:00
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
2020-03-28 12:42:32 +13:00
|
|
|
|
namespace SystemTrayMenu.UserInterface
|
2019-07-05 05:04:14 +12:00
|
|
|
|
{
|
|
|
|
|
public class FadeForm : IDisposable
|
|
|
|
|
{
|
2020-03-24 00:41:10 +13:00
|
|
|
|
public bool IsFadingIn => timerFadeIn.Enabled;
|
2019-07-05 05:04:14 +12:00
|
|
|
|
|
2020-03-24 00:41:10 +13:00
|
|
|
|
public bool IsFadingOut => timerFadeOut.Enabled;
|
2019-07-05 05:04:14 +12:00
|
|
|
|
|
2020-03-24 00:35:10 +13:00
|
|
|
|
private readonly Timer timerFadeIn = new Timer();
|
|
|
|
|
private readonly Timer timerFadeOut = new Timer();
|
|
|
|
|
private readonly Timer timerFadeHalf = new Timer();
|
|
|
|
|
private readonly Form form = null;
|
2020-03-30 06:26:45 +13:00
|
|
|
|
private readonly bool disposeForm = false;
|
2020-03-24 00:32:47 +13:00
|
|
|
|
private bool stopFadeInByHalf = false;
|
2019-07-05 05:04:14 +12:00
|
|
|
|
|
2020-03-30 06:26:45 +13:00
|
|
|
|
public FadeForm(Form form, bool disposeForm = false)
|
2019-07-05 05:04:14 +12:00
|
|
|
|
{
|
|
|
|
|
this.form = form;
|
2020-03-30 06:26:45 +13:00
|
|
|
|
this.disposeForm = disposeForm;
|
2019-07-05 05:04:14 +12:00
|
|
|
|
timerFadeIn.Interval = MenuDefines.IntervalFade;
|
|
|
|
|
timerFadeOut.Interval = MenuDefines.IntervalFade;
|
|
|
|
|
timerFadeHalf.Interval = MenuDefines.IntervalFade;
|
|
|
|
|
timerFadeIn.Tick += TimerFadeIn_Tick;
|
|
|
|
|
timerFadeOut.Tick += TimerFadeOut_Tick;
|
|
|
|
|
timerFadeHalf.Tick += TimerFadeHalf_Tick;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
Dispose(true);
|
2020-03-28 12:42:32 +13:00
|
|
|
|
GC.SuppressFinalize(this);
|
2019-07-05 05:04:14 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
|
|
|
{
|
|
|
|
|
if (disposing)
|
|
|
|
|
{
|
|
|
|
|
timerFadeIn.Dispose();
|
|
|
|
|
timerFadeOut.Dispose();
|
|
|
|
|
timerFadeHalf.Dispose();
|
2020-03-30 06:26:45 +13:00
|
|
|
|
if (disposeForm)
|
|
|
|
|
{
|
|
|
|
|
form.Dispose();
|
|
|
|
|
}
|
2019-07-05 05:04:14 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void FadeOut()
|
|
|
|
|
{
|
|
|
|
|
timerFadeHalf.Stop();
|
|
|
|
|
timerFadeIn.Stop();
|
|
|
|
|
timerFadeOut.Start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void FadeHalf()
|
|
|
|
|
{
|
|
|
|
|
if (form.Visible &&
|
|
|
|
|
form.Opacity < MenuDefines.OpacityHalfValue)
|
|
|
|
|
{
|
|
|
|
|
timerFadeOut.Stop();
|
|
|
|
|
timerFadeHalf.Stop();
|
2020-03-21 23:14:16 +13:00
|
|
|
|
stopFadeInByHalf = true;
|
2019-07-05 05:04:14 +12:00
|
|
|
|
timerFadeIn.Start();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
timerFadeIn.Stop();
|
|
|
|
|
timerFadeOut.Stop();
|
|
|
|
|
timerFadeHalf.Start();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void FadeIn()
|
|
|
|
|
{
|
|
|
|
|
stopFadeInByHalf = false;
|
|
|
|
|
if (form.Visible)
|
|
|
|
|
{
|
|
|
|
|
timerFadeOut.Stop();
|
|
|
|
|
timerFadeHalf.Stop();
|
|
|
|
|
timerFadeIn.Start();
|
|
|
|
|
}
|
2020-03-17 09:05:52 +13:00
|
|
|
|
else
|
2019-07-05 05:04:14 +12:00
|
|
|
|
{
|
2020-03-30 06:26:45 +13:00
|
|
|
|
DllImports.NativeMethods.User32ShowInactiveTopmost(form);
|
2019-07-05 05:04:14 +12:00
|
|
|
|
timerFadeOut.Stop();
|
|
|
|
|
timerFadeIn.Start();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void TimerFadeIn_Tick(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (form.Opacity >= 1 ||
|
|
|
|
|
(stopFadeInByHalf && form.Opacity >= MenuDefines.OpacityHalfValue))
|
|
|
|
|
{
|
|
|
|
|
if (stopFadeInByHalf)
|
|
|
|
|
{
|
|
|
|
|
form.Opacity = MenuDefines.OpacityHalfValue;
|
|
|
|
|
stopFadeInByHalf = false;
|
|
|
|
|
}
|
|
|
|
|
timerFadeIn.Stop();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
form.Opacity += MenuDefines.OpacityInStep;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void TimerFadeOut_Tick(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (form.Opacity < 0.01)
|
|
|
|
|
{
|
|
|
|
|
form.Hide();
|
|
|
|
|
timerFadeOut.Stop();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
form.Opacity -= MenuDefines.OpacityOutStep;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void TimerFadeHalf_Tick(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (form.Opacity <= MenuDefines.OpacityHalfValue)
|
|
|
|
|
{
|
|
|
|
|
form.Opacity = MenuDefines.OpacityHalfValue;
|
|
|
|
|
timerFadeHalf.Stop();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
form.Opacity -= MenuDefines.OpacityHalfStep;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|