using System; using System.Threading.Tasks; using System.Windows.Forms; namespace FormFader { /// /// An object used to control fading forms in and out. /// class Fader { // This delegate is later used as a call back for // when the form has completed fading. public delegate void FadeCompleted(); // Class properties private readonly Form form; // The form to modify the opacity of. private readonly Form parentForm; // The parent form if being displayed as a dialog. private FadeDirection fadeDirection; // The direction in which to fade. private float fadeSpeed; // The speed at which to fade. private FadeCompleted fadeFinished; // The delegate to call when a fade has completed. private bool shouldClose; // If set to true, the form will close after fading out. private readonly TaskCompletionSource showDialogResult // The Async Task Completion Source for displaying as a dialog. = new TaskCompletionSource(); // Enum for controlling the fade direction. private enum FadeDirection { In, Out } // Constants for setting the fade speed. // (You can alternately use a custom float value) public static class FadeSpeed { public static readonly float Slowest = 1; public static readonly float Slower = 10; public static readonly float Slow = 25; public static readonly float Normal = 50; public static readonly float Fast = 60; public static readonly float Faster = 75; public static readonly float Fastest = 100; } /// /// Construct the Fader object with a form. /// private Fader(Form form) { this.form = form; } /// /// Construct a Fader object with a form and a parent form. /// private Fader(Form form, Form parent) : this(form) { this.parentForm = parent; } /// /// Begin fading the form. /// private void BeginFade() { UpdateOpacity(); fadeFinished?.Invoke(); } /// /// Update the opacity of the form using the timer. /// private async void UpdateOpacity() { if (form.IsDisposed) { return; } switch (fadeDirection) { // Fade in case FadeDirection.In: if (form.Opacity < 1.0) form.Opacity += (fadeSpeed / 1000.0); else return; break; // Fade out case FadeDirection.Out: if (form.Opacity > 0.1) { form.Opacity -= (fadeSpeed / 1000.0); } else { if (!shouldClose) form.Hide(); else form.Close(); return; } break; } await Task.Delay(10); UpdateOpacity(); } /// /// Fade the form in at the defined speed as a dialog /// based on parent form. /// private async Task ShowDialog(float fadeSpeed, FadeCompleted finished) { parentForm.BeginInvoke(new Action(() => showDialogResult.SetResult(form.ShowDialog(parentForm)))); fadeFinished = finished; form.Opacity = 0; this.fadeSpeed = fadeSpeed; fadeDirection = FadeDirection.In; BeginFade(); return await showDialogResult.Task; } /// /// Fade the form in at the defined speed. /// private void FadeIn(float fadeSpeed, FadeCompleted finished) { form.Opacity = 0; form.Show(); fadeFinished = finished; this.fadeSpeed = fadeSpeed; fadeDirection = FadeDirection.In; BeginFade(); } /// /// Fade the form out at the defined speed. /// private void FadeOut(float fadeSpeed, FadeCompleted finished) { if (form.Opacity < 0.1) { finished?.Invoke(); return; } fadeFinished = finished; form.Opacity = 100; this.fadeSpeed = fadeSpeed; fadeDirection = FadeDirection.Out; BeginFade(); } /// /// Fades a dialog in using parent form and defined fade speed. /// public async static Task ShowDialog(Form form, Form parent, float fadeSpeed) { Fader fader = new Fader(form, parent); return await fader.ShowDialog(fadeSpeed, null); } /// /// Fades a dialog in using parent form and defined fade speed /// and call the finished delegate.) /// public async static Task ShowDialog(Form form, Form parent, float fadeSpeed, FadeCompleted finished) { Fader fader = new Fader(form, parent); return await fader.ShowDialog(fadeSpeed, finished); } /// /// Fade a form in at the defined speed. /// public static void FadeIn(Form form, float fadeSpeed, FadeCompleted finished) { Fader fader = new Fader(form); fader.FadeIn(fadeSpeed, finished); } /// /// Fade a form out at the defined speed. /// public static void FadeOut(Form form, float fadeSpeed, FadeCompleted finished) { Fader fader = new Fader(form); fader.FadeOut(fadeSpeed, finished); } /// /// Fade a form in at the defined speed. /// public static void FadeIn(Form form, float fadeSpeed) { Fader fader = new Fader(form); fader.FadeIn(fadeSpeed, null); } /// /// Fade a form out at the defined speed. /// public static void FadeOut(Form form, float fadeSpeed) { Fader fader = new Fader(form); fader.FadeOut(fadeSpeed, null); } /// /// Fade a form out at the defined speed and /// close it when the fade has completed. /// public static void FadeOutAndClose(Form form, float fadeSpeed) { Fader fader = new Fader(form) { shouldClose = true }; fader.FadeOut(fadeSpeed, null); } /// /// Fade a form out at the defined speed and /// close it when the fade has completed. /// After the form has closed, call the FadeComplete delegate. /// public static void FadeOutAndClose(Form form, float fadeSpeed, FadeCompleted finished) { Fader fader = new Fader(form) { shouldClose = true }; fader.FadeOut(fadeSpeed, finished); } } }