public static extern IntPtr BeginBufferedAnimation( IntPtr hwnd, IntPtr hdcTarget, ref Rectangle rcTarget, BP_BUFFERFORMAT dwFormat, IntPtr pPaintParams, ref BP_ANIMATIONPARAMS pAnimationParams, out IntPtr phdcFrom, out IntPtr phdcTo );
private void Control_Paint(object sender, PaintEventArgs e) { if (BufferedPaintSupported) { var stateChanged = !Equals(currentState, newState); using (var hdc = new SafeDCHandle(e.Graphics)) { if (!hdc.IsInvalid) { // see if this paint was generated by a soft-fade animation if (!BufferedPaintRenderAnimation(new HandleRef(Control, Control.Handle), hdc)) { var animParams = new BP_ANIMATIONPARAMS(BP_ANIMATIONSTYLE.BPAS_LINEAR); // get appropriate animation time depending on state transition (or 0 if unchanged) if (stateChanged) { var transition = Transitions.SingleOrDefault(x => Equals(x.FromState, currentState) && Equals(x.ToState, newState)); animParams.Duration = transition?.Duration ?? DefaultDuration; } using (var h = new BufferedPaintHandle(Control, hdc, Control.ClientRectangle, animParams, BP_PAINTPARAMS.NoClip)) { if (!h.IsInvalid) { if (h.SourceGraphics != null) { OnPaintVisualState(new BufferedPaintEventArgs <TState>(currentState, h.SourceGraphics)); } if (h.Graphics != null) { OnPaintVisualState(new BufferedPaintEventArgs <TState>(newState, h.Graphics)); } } else { currentState = newState; OnPaintVisualState(new BufferedPaintEventArgs <TState>(currentState, e.Graphics)); } } } } } } else { // buffered painting not supported, just paint using the current state currentState = newState; OnPaintVisualState(new BufferedPaintEventArgs <TState>(currentState, e.Graphics)); } }
/// <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}."); }
public static extern SafeBufferedAnimationHandle BeginBufferedAnimation(HandleRef hwnd, SafeDCHandle hdcTarget, [In] ref RECT rcTarget, BP_BUFFERFORMAT dwFormat, [In] BP_PAINTPARAMS pPaintParams, [In] ref BP_ANIMATIONPARAMS pAnimationParams, out IntPtr phdcFrom, out IntPtr phdcTo);