using System; using System.Drawing; using System.Runtime.InteropServices; using Vanara.Extensions; using static Vanara.PInvoke.Gdi32; // ReSharper disable FieldCanBeMadeReadOnly.Global // ReSharper disable InconsistentNaming namespace Vanara.PInvoke { public static partial class UxTheme { /// Used in the BP_ANIMATIONPARAMS structure to declare animation options. [PInvokeData("UxTheme.h")] [Flags] public enum BP_ANIMATIONSTYLE { /// No animation. Not implemented in Windows Vista. BPAS_NONE, /// Linear fade animation. BPAS_LINEAR, /// Cubic fade animation. Not implemented in Windows Vista. BPAS_CUBIC, /// Sinusoid fade animation. Not implemented in Windows Vista. BPAS_SINE } /// Specifies the format of the buffer. Used by BeginBufferedAnimation and BeginBufferedPaint. [PInvokeData("UxTheme.h")] public enum BP_BUFFERFORMAT { /// Compatible bitmap. The number of bits per pixel is based on the color format of the device associated with the HDC specified with BeginBufferedPaint or BeginBufferedAnimation—typically, this is the display device. BPBF_COMPATIBLEBITMAP, /// Bottom-up device-independent bitmap. The origin of the bitmap is the lower-left corner. Uses 32 bits per pixel. BPBF_DIB, /// Top-down device-independent bitmap. The origin of the bitmap is the upper-left corner. Uses 32 bits per pixel. BPBF_TOPDOWNDIB, /// Top-down, monochrome, device-independent bitmap. Uses 1 bit per pixel. BPBF_TOPDOWNMONODIB } /// Used in BP_PAINTPARAMS [PInvokeData("UxTheme.h")] [Flags] public enum BufferedPaintParamsFlags { /// No flag. BPPF_NONE = 0, /// Initialize the buffer to ARGB = {0, 0, 0, 0} during BeginBufferedPaint. This erases the previous contents of the buffer. BPPF_ERASE = 1, /// Do not apply the clip region of the target DC to the double buffer. If this flag is not set and if the target DC is a window DC, then clipping due to overlapping windows is applied to the double buffer. BPPF_NOCLIP = 2, /// A non-client DC is being used. BPPF_NONCLIENT = 4, } /// Begins a buffered animation operation. The animation consists of a cross-fade between the contents of two buffers over a specified period of time. /// A handle to the window in which the animations play. /// A handle of the target DC on which the buffer is animated. /// A pointer to a structure that specifies the area of the target DC in which to draw. /// The format of the buffer. /// A pointer to a structure that defines the paint operation parameters. This value can be NULL. /// A pointer to a structure that defines the animation operation parameters. /// When this function returns, this value points to the handle of the DC where the application should paint the initial state of the animation, if not NULL. /// When this function returns, this value points to the handle of the DC where the application should paint the final state of the animation, if not NULL. /// A handle to the buffered paint animation. [PInvokeData("UxTheme.h")] [DllImport(Lib.UxTheme, ExactSpelling = true, SetLastError = true)] public static extern IntPtr 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); /// Begins a buffered paint operation. /// The handle of the target DC on which the buffer will be painted. /// A pointer to a RECT structure that specifies the area of the target DC in which to paint. /// A member of the BP_BUFFERFORMAT enumeration that specifies the format of the buffer. /// A pointer to a BP_PAINTPARAMS structure that defines the paint operation parameters. This value can be NULL. /// When this function returns, points to the handle of the new device context. /// A handle to the buffered paint context. If this function fails, the return value is NULL, and phdc is NULL. To get extended error information, call GetLastError. /// The returned handle is freed when EndBufferedPaint is called. /// An application should call BufferedPaintInit on the calling thread before calling BeginBufferedPaint, and BufferedPaintUnInit before the thread is terminated.Failure to call BufferedPaintInit may result in degraded performance due to internal data being initialized and destroyed for each buffered paint operation. [PInvokeData("UxTheme.h")] [DllImport(Lib.UxTheme, ExactSpelling = true, SetLastError = true)] public static extern IntPtr BeginBufferedPaint(SafeDCHandle hdcTarget, [In] ref RECT prcTarget, BP_BUFFERFORMAT dwFormat, [In] BP_PAINTPARAMS pPaintParams, out IntPtr phdc); /// Clears a specified rectangle in the buffer to ARGB = {0,0,0,0}. /// The handle of the buffered paint context, obtained through BeginBufferedPaint. /// A pointer to a RECT structure that specifies the rectangle to clear. Set this parameter to NULL to specify the entire buffer. /// If this function succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code. [PInvokeData("UxTheme.h")] [DllImport(Lib.UxTheme, ExactSpelling = true)] public static extern HRESULT BufferedPaintClear(IntPtr hBufferedPaint, ref RECT prc); /// Initialize buffered painting for the current thread. /// If this function succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code. [PInvokeData("UxTheme.h")] [DllImport(Lib.UxTheme, ExactSpelling = true)] public static extern HRESULT BufferedPaintInit(); /// Paints the next frame of a buffered paint animation. /// Handle to the window in which the animations play. /// Handle of the target DC on which the buffer is animated. /// Returns TRUE if the frame has been painted, or FALSE otherwise. [PInvokeData("UxTheme.h")] [DllImport(Lib.UxTheme, ExactSpelling = true)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool BufferedPaintRenderAnimation(HandleRef hwnd, SafeDCHandle hdcTarget); /// Sets the alpha to a specified value in a given rectangle. The alpha controls the amount of transparency applied when blending with the buffer onto the destination target device context (DC). /// The handle of the buffered paint context, obtained through BeginBufferedPaint. /// A pointer to a RECT structure that specifies the rectangle in which to set the alpha. Set this parameter to NULL to specify the entire buffer. /// The alpha value to set. The alpha value can range from zero (fully transparent) to 255 (fully opaque). /// If this function succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code. [PInvokeData("UxTheme.h")] [DllImport(Lib.UxTheme, ExactSpelling = true)] public static extern HRESULT BufferedPaintSetAlpha(IntPtr hBufferedPaint, ref RECT prc, byte alpha); /// Stops all buffered animations for the given window. /// The handle of the window in which to stop all animations. /// If this function succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code. [PInvokeData("UxTheme.h")] [DllImport(Lib.UxTheme, ExactSpelling = true)] public static extern HRESULT BufferedPaintStopAllAnimations(HandleRef hwnd); /// Closes down buffered painting for the current thread. Called once for each call to BufferedPaintInit after calls to BeginBufferedPaint are no longer needed. /// If this function succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code. [PInvokeData("UxTheme.h")] [DllImport(Lib.UxTheme, ExactSpelling = true)] public static extern HRESULT BufferedPaintUnInit(); /// Renders the first frame of a buffered animation operation and starts the animation timer. /// The handle to the buffered animation context that was returned by BeginBufferedAnimation. /// If TRUE, updates the target DC with the animation. If FALSE, the animation is not started, the target DC is not updated, and the hbpAnimation parameter is freed. /// If this function succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code. [PInvokeData("UxTheme.h")] [DllImport(Lib.UxTheme, ExactSpelling = true)] public static extern HRESULT EndBufferedAnimation(IntPtr hbpAnimation, [MarshalAs(UnmanagedType.Bool)] bool fUpdateTarget); /// Completes a buffered paint operation and frees the associated buffered paint handle. /// The handle of the buffered paint context, obtained through BeginBufferedPaint. /// TRUE to copy the buffer to the target DC. /// If this function succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code. [PInvokeData("UxTheme.h")] [DllImport(Lib.UxTheme, ExactSpelling = true)] public static extern HRESULT EndBufferedPaint(IntPtr hbp, [MarshalAs(UnmanagedType.Bool)] bool fUpdateTarget); /// Retrieves a pointer to the buffer bitmap if the buffer is a device-independent bitmap (DIB). /// The handle of the buffered paint context, obtained through BeginBufferedPaint. /// When this function returns, contains a pointer to the address of the buffer bitmap pixels. /// When this function returns, contains a pointer to the width, in pixels, of the buffer bitmap. This value is not necessarily equal to the buffer width. It may be larger. /// Returns S_OK if successful, or an error value otherwise. If an error occurs, ppbBuffer is set to NULL and pcxRow is set to zero. [PInvokeData("UxTheme.h")] [DllImport(Lib.UxTheme, ExactSpelling = true)] public static extern HRESULT GetBufferedPaintBits(IntPtr hBufferedPaint, out IntPtr ppbBuffer, out int pcxRow); /// Gets the paint device context (DC). This is the same value retrieved by BeginBufferedPaint. /// Handle of the buffered paint context, obtained through BeginBufferedPaint. /// Handle of the requested DC. This is the same DC that is returned by BeginBufferedPaint. Returns NULL upon failure. [PInvokeData("UxTheme.h")] [DllImport(Lib.UxTheme, ExactSpelling = true)] public static extern IntPtr GetBufferedPaintDC(IntPtr hBufferedPaint); /// Retrieves the target device context (DC). /// A handle to the buffered paint context obtained through BeginBufferedPaint. /// A handle to the requested DC, or NULL otherwise. [PInvokeData("UxTheme.h")] [DllImport(Lib.UxTheme, ExactSpelling = true)] public static extern IntPtr GetBufferedPaintTargetDC(IntPtr hBufferedPaint); /// Retrieves the target rectangle specified by BeginBufferedPaint. /// Handle to the buffered paint context obtained through BeginBufferedPaint. /// When this function returns, contains the requested rectangle. /// If this function succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code. [PInvokeData("UxTheme.h")] [DllImport(Lib.UxTheme, ExactSpelling = true)] public static extern HRESULT GetBufferedPaintTargetRect(IntPtr hBufferedPaint, out RECT prc); /// Defines animation parameters for the BP_PAINTPARAMS structure used by BeginBufferedPaint. [PInvokeData("UxTheme.h")] [StructLayout(LayoutKind.Sequential)] public struct BP_ANIMATIONPARAMS { /// The size, in bytes, of this structure. public uint cbSize; /// Reserved. public uint dwFlags; /// Animation style. public BP_ANIMATIONSTYLE style; /// Length of the animation, in milliseconds. public uint dwDuration; /// Initializes a new instance of the struct. /// The animation style. /// The duration. public BP_ANIMATIONPARAMS(BP_ANIMATIONSTYLE animStyle, int dur = 0) { cbSize = (uint)Marshal.SizeOf(typeof(BP_ANIMATIONPARAMS)); dwFlags = 0; dwDuration = (uint)dur; style = animStyle; } /// Gets an instance of an empty structure with cbSize set. public static BP_ANIMATIONPARAMS Empty => new BP_ANIMATIONPARAMS { cbSize = (uint)Marshal.SizeOf(typeof(BP_ANIMATIONPARAMS)) }; } /// Defines paint operation parameters for BeginBufferedPaint. /// [PInvokeData("UxTheme.h")] [StructLayout(LayoutKind.Sequential)] public class BP_PAINTPARAMS : IDisposable { /// The size, in bytes, of this structure. public int cbSize; /// One or more of the following values. public BufferedPaintParamsFlags Flags; /// A pointer to exclusion RECT structure. This rectangle is excluded from the clipping region. May be NULL for no exclusion rectangle. public IntPtr prcExclude; /// A pointer to BLENDFUNCTION structure, which controls blending by specifying the blending functions for source and destination bitmaps. If NULL, the source buffer is copied to the destination with no blending. public IntPtr pBlendFunction; /// Initializes a new instance of the class. /// The flags. public BP_PAINTPARAMS(BufferedPaintParamsFlags flags = BufferedPaintParamsFlags.BPPF_NONE) { cbSize = Marshal.SizeOf(typeof(BP_PAINTPARAMS)); Flags = flags; prcExclude = pBlendFunction = IntPtr.Zero; } /// Gets or sets the rectangle that is excluded from the clipping region. /// The rectangle. public Rectangle? Exclude { get => prcExclude.ToNullableStructure(); set { if (prcExclude != IntPtr.Zero) Marshal.FreeCoTaskMem(prcExclude); if (value.HasValue && !value.Value.IsEmpty) prcExclude = value.StructureToPtr(Marshal.AllocCoTaskMem, out int _); } } /// Gets or sets the blend function. /// The blend function. public BLENDFUNCTION? BlendFunction { get => pBlendFunction.ToNullableStructure(); set { if (pBlendFunction != IntPtr.Zero) Marshal.FreeCoTaskMem(pBlendFunction); if (value.HasValue && !value.Value.IsEmpty) pBlendFunction = value.StructureToPtr(Marshal.AllocCoTaskMem, out int _); } } /// /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// public void Dispose() { if (prcExclude != IntPtr.Zero) Marshal.FreeCoTaskMem(prcExclude); if (pBlendFunction != IntPtr.Zero) Marshal.FreeCoTaskMem(pBlendFunction); } /// Gets an instance of this structure set to define no clipping. public static BP_PAINTPARAMS NoClip => new BP_PAINTPARAMS(BufferedPaintParamsFlags.BPPF_NOCLIP); /// Gets an instance of this structure set to define clearing the background. public static BP_PAINTPARAMS ClearBg => new BP_PAINTPARAMS(BufferedPaintParamsFlags.BPPF_ERASE); } } }