public static void Paint <TState, TParam>(Graphics g, IWin32Window w, Rectangle rc, PaintAction <TState, TParam> f, TState currentState, TState newState, uint duration, TParam optParam) { try { if (System.Environment.OSVersion.Version.Major >= 6 && NativeMethods.BufferedPaintInit() == IntPtr.Zero) { using (var hdc = new SafeGDIHandle(g)) { if (!hdc.IsInvalid) { // see if this paint was generated by a soft-fade animation if (!NativeMethods.BufferedPaintRenderAnimation(w.Handle, hdc)) { NativeMethods.BufferedPaintAnimationParams animParams = new NativeMethods.BufferedPaintAnimationParams { Duration = duration }; IntPtr hdcFrom, hdcTo; using (var h = new BufferedPaintHandle(w.Handle, hdc, ref rc, ref animParams, out hdcFrom, out hdcTo)) { if (!h.IsInvalid) { if (hdcFrom != IntPtr.Zero) { using (Graphics gfxFrom = Graphics.FromHdc(hdcFrom)) f(gfxFrom, rc, currentState, optParam); } if (hdcTo != IntPtr.Zero) { using (Graphics gfxTo = Graphics.FromHdc(hdcTo)) f(gfxTo, rc, newState, optParam); } } else { hdc.Dispose(); f(g, rc, newState, optParam); } } } } } NativeMethods.BufferedPaintUnInit(); } else { f(g, rc, newState, optParam); } } catch { } }