/// <summary>Performs a buffered animation operation. The animation consists of a cross-fade between the contents of two buffers over a specified period of time.</summary> /// <typeparam name="TState">The type of the state that is used to determine the image to paint.</typeparam> /// <typeparam name="TParam">The type of the parameter that is passed into this method.</typeparam> /// <param name="graphics">The target DC on which the buffer is animated.</param> /// <param name="ctrl">The window in which the animations play.</param> /// <param name="bounds">Specifies the area of the target DC in which to draw.</param> /// <param name="paintAction">A method delegate that performs the painting of the control at a given state.</param> /// <param name="currentState">The current state to use to start drawing the animation.</param> /// <param name="newState">The final state to use to finish drawing the animation.</param> /// <param name="getDuration">A method delegate that gets the duration of the animation, in milliseconds.</param> /// <param name="data">User-defined data to pass to the <paramref name="paintAction"/> callback.</param> public static void PaintAnimation <TState, TParam>(Graphics graphics, IWin32Window ctrl, Rectangle bounds, PaintAction <TState, TParam> paintAction, TState currentState, TState newState, GetDuration <TState> getDuration, TParam data) { try { if (System.Environment.OSVersion.Version.Major >= 6) { // If this handle is running with a different state, stop the animations if (paintAnimationInstances.TryGetValue(ctrl.Handle, out Tuple <object, object> val)) { if (!Equals(val.Item1, currentState) || !Equals(val.Item2, newState)) { BufferedPaintStopAllAnimations(new HandleRef(ctrl, ctrl.Handle)); System.Diagnostics.Debug.WriteLine("BufferedPaintStop."); paintAnimationInstances[ctrl.Handle] = new Tuple <object, object>(currentState, newState); } } else { paintAnimationInstances.Add(ctrl.Handle, new Tuple <object, object>(currentState, newState)); } using (var hdc = new SafeDCHandle(graphics)) { if (hdc.IsInvalid) { return; } // see if this paint was generated by a soft-fade animation if (BufferedPaintRenderAnimation(new HandleRef(ctrl, ctrl.Handle), hdc)) { paintAnimationInstances.Remove(ctrl.Handle); return; } var animParams = new BP_ANIMATIONPARAMS(BP_ANIMATIONSTYLE.BPAS_LINEAR, getDuration?.Invoke(currentState, newState) ?? 0); using (var h = new BufferedAnimationPainter(ctrl, hdc, bounds, animParams, BP_PAINTPARAMS.NoClip)) { if (!h.IsInvalid) { if (h.SourceGraphics != null) { paintAction(h.SourceGraphics, bounds, currentState, data); } if (h.DestinationGraphics != null) { paintAction(h.DestinationGraphics, bounds, newState, data); } } else { // hdc.Dispose(); paintAction(graphics, bounds, newState, data); } } } } else { paintAction(graphics, bounds, newState, data); } } catch { } System.Diagnostics.Debug.WriteLine($"BufferedPaint state items = {paintAnimationInstances.Count}."); }
/// <summary>Performs a buffered animation operation. The animation consists of a cross-fade between the contents of two buffers over a specified period of time.</summary> /// <typeparam name="TState">The type of the state that is used to determine the image to paint.</typeparam> /// <param name="graphics">The target DC on which the buffer is animated.</param> /// <param name="ctrl">The window in which the animations play.</param> /// <param name="bounds">Specifies the area of the target DC in which to draw.</param> /// <param name="paintAction">A method delegate that performs the painting of the control at a given state.</param> /// <param name="currentState">The current state to use to start drawing the animation.</param> /// <param name="newState">The final state to use to finish drawing the animation.</param> /// <param name="getDuration">A method delegate that gets the duration of the animation, in milliseconds.</param> public static void PaintAnimation <TState>(Graphics graphics, IWin32Window ctrl, Rectangle bounds, PaintAction <TState, int> paintAction, TState currentState, TState newState, GetDuration <TState> getDuration) => PaintAnimation(graphics, ctrl, bounds, paintAction, currentState, newState, getDuration, 0);