/// <summary> /// Animates the <see cref="Control.BackColor"/> alpha channel /// </summary> /// <param name="form">A <see cref="Control"/></param> /// <param name="from">A starting position, if null, it will use the current opacity value</param> /// <param name="to">The final result, if null, it will be set to 1.0</param> /// <param name="duration">The duration for animation to complete</param> /// <param name="easingFunction">A defined <see cref="EasingFunction"/> to interpolate the animation values</param> public static void AnimateOpacity(this Control control, double duration, double?from, double?to, EasingFunction easingFunction = null) { if (control != null) { if (!from.HasValue) { from = 0.0; } if (!to.HasValue) { to = 1.0; } // ensure the UI thread updates the UI components Action <double> invoker = new Action <double>((value) => { control.BackColor = Color.FromArgb((int)value * 255, control.BackColor.R, control.BackColor.G, control.BackColor.B); }); DoubleAnimation doubleAnimation = new DoubleAnimation(from.Value, to.Value, easingFunction ?? ControlPropertyAnimator.DefaultEasingFunction, duration); doubleAnimation.TimelineTick += (sender, args) => { double currentValue = doubleAnimation.Current; control.BeginInvoke(invoker, currentValue); }; doubleAnimation.Begin(); } }
/// <summary> /// Animates the <see cref="Form.Opacity"/> property with the given control parameters /// </summary> /// <param name="form">The <see cref="Form"/> to animate the opacity property</param> /// <param name="from">A starting position, if null, it will use the current opacity value</param> /// <param name="to">The final result, if null, it will be set to 1.0</param> /// <param name="duration">The duration for animation to complete</param> /// <param name="easingFunction">A defined <see cref="EasingFunction"/> to interpolate the animation values</param> public static void AnimateOpacity(this Form form, double duration, double?from, double?to, EasingFunction easingFunction = null) { if (form != null) { if (!from.HasValue) { from = form.Opacity; } if (!to.HasValue) { to = 1.0; } // ensure the UI thread updates the UI components Action <double> invoker = new Action <double>((value) => { form.Opacity = value; }); DoubleAnimation doubleAnimation = new DoubleAnimation(from.Value, to.Value, easingFunction ?? ControlPropertyAnimator.DefaultEasingFunction, duration); doubleAnimation.TimelineTick += (sender, args) => { double currentValue = doubleAnimation.Current; form.BeginInvoke(invoker, currentValue); }; doubleAnimation.Begin(); } }
/// <summary> /// Animates a <see cref="Control"/> by adjusting the width and height whilst maintaining center position. /// </summary> /// <param name="control">A <see cref="Control"/></param> /// <param name="from">A starting position, if null, it will use the current opacity value</param> /// <param name="to">The final result, if null, it will be set to 1.0</param> /// <param name="duration">The duration for animation to complete</param> /// <param name="easingFunction">A defined <see cref="EasingFunction"/> to interpolate the animation values</param> public static void AnimateScale(this Control control, double duration, Size?from, Size?to, ScaleMode scaleMode, EasingFunction easingFunction = null) { if (control != null) { if (!from.HasValue) { from = new Size(control.Width, control.Height); } if (!to.HasValue) { to = new Size(control.Width, control.Height); } // ensure the UI thread updates the UI components Action <Size> invoker = new Action <Size>((value) => { scaleMode.Apply(control, value); }); double _fromFactor = 0.0; double _toFactor = 1.0; DoubleAnimation doubleAnimation = new DoubleAnimation(_fromFactor, _toFactor, easingFunction ?? ControlPropertyAnimator.DefaultEasingFunction, duration); doubleAnimation.TimelineTick += (sender, args) => { double currentValue = doubleAnimation.Current; // create a size object based from the current factor Size _size = new Size((int)(to.Value.Width * currentValue), (int)(to.Value.Height * currentValue)); control.BeginInvoke(invoker, _size); }; control.Size = from.Value; doubleAnimation.Begin(); } }
/// <summary> /// Animates this prompt in with the specified options /// </summary> public DialogResult Show(string message, string title, PromptMessageButtons buttons) { this.ParentControl.BringToFront(); this.Title.Text = title; this.Message.Text = message; switch (buttons) { case PromptMessageButtons.Ok: this.Btn2.Text = DialogResult.OK.ToString(); this.Btn2.Visible = true; this.Btn2.BackColor = DialogButtonStyles.PositiveBackColor; this.Btn1.Visible = false; this.Btn3.Visible = false; break; case PromptMessageButtons.OkCancel: this.Btn1.Text = DialogResult.OK.ToString(); this.Btn1.Visible = true; this.Btn1.BackColor = DialogButtonStyles.PositiveBackColor; this.Btn3.Text = DialogResult.Cancel.ToString(); this.Btn3.Visible = true; this.Btn3.BackColor = DialogButtonStyles.NeutralBackColor; this.Btn2.Visible = false; break; case PromptMessageButtons.YesNoCancel: this.Btn1.Text = DialogResult.Yes.ToString(); this.Btn1.Visible = true; this.Btn1.BackColor = DialogButtonStyles.PositiveBackColor; this.Btn2.Text = DialogResult.Cancel.ToString(); this.Btn2.Visible = true; this.Btn2.BackColor = DialogButtonStyles.NeutralBackColor; this.Btn3.Text = DialogResult.No.ToString(); this.Btn3.Visible = true; this.Btn3.BackColor = DialogButtonStyles.NegativeBackColor; break; case PromptMessageButtons.YesNo: this.Btn1.Text = DialogResult.Yes.ToString(); this.Btn1.Visible = true; this.Btn1.BackColor = DialogButtonStyles.PositiveBackColor; this.Btn3.Text = DialogResult.No.ToString(); this.Btn3.Visible = true; this.Btn3.BackColor = DialogButtonStyles.NegativeBackColor; this.Btn2.Visible = false; break; default: break; } int _scale = 45; int _y = this.PromptPanel.Location.Y - _scale; this.PromptPanel.Location = new Point(this.PromptPanel.Location.X, _y); // ensure the UI thread updates the UI components Action <double> invoker = new Action <double>((value) => { this.PromptPanel.Location = new Point(this.PromptPanel.Location.X, _y + (int)(_scale * value)); }); DoubleAnimation doubleAnimation = new DoubleAnimation(0.0, 1.0, new QuadraticEaseFunction(EaseMode.EaseOut), 300, 120); doubleAnimation.TimelineTick += (o, a) => { double _current = doubleAnimation.Current; this.BeginInvoke(invoker, _current); }; doubleAnimation.Begin(); this.AnimateOpacity(300, 0.0, 0.6); return(base.ShowDialog()); }