public BufferedPaintHandle(IWin32Window wnd, SafeDCHandle hdc, Rectangle targetRectangle, BP_ANIMATIONPARAMS?animationParams = null, BP_PAINTPARAMS paintParams = null, BP_BUFFERFORMAT fmt = BP_BUFFERFORMAT.BPBF_TOPDOWNDIB) : base(IntPtr.Zero, true) { RECT rc = targetRectangle; var ap = animationParams ?? BP_ANIMATIONPARAMS.Empty; IntPtr hdcFrom, hdcTo; var hbp = BeginBufferedAnimation(new HandleRef(wnd, wnd.Handle), hdc, ref rc, fmt, paintParams, ref ap, out hdcFrom, out hdcTo); if (hbp == IntPtr.Zero) { throw new Win32Exception(); } if (hdcFrom != IntPtr.Zero) { SourceGraphics = Graphics.FromHdc(hdcFrom); } if (hdcTo != IntPtr.Zero) { Graphics = Graphics.FromHdc(hdcTo); } SetHandle(hbp); ani = true; }
/// <summary>Initializes a new instance of the <see cref="BufferedPainter"/> class and begins a buffered paint operation.</summary> /// <param name="hdc">The handle of the target DC on which the buffer will be painted.</param> /// <param name="targetRectangle">Specifies the area of the target DC in which to paint.</param> /// <param name="paintParams">The paint operation parameters. This value can be <see langword="null"/>.</param> /// <param name="fmt">The format of the buffer.</param> /// <exception cref="Win32Exception">Buffered painting could not initialize.</exception> public BufferedPainter(SafeDCHandle hdc, Rectangle targetRectangle, BP_PAINTPARAMS paintParams = null, BP_BUFFERFORMAT fmt = BP_BUFFERFORMAT.BPBF_TOPDOWNDIB) { RECT target = targetRectangle; hbp = BeginBufferedPaint(hdc, ref target, fmt, paintParams, out IntPtr phdc); if (hbp.IsInvalid) { throw new Win32Exception(); } if (phdc != IntPtr.Zero) { Graphics = Graphics.FromHdc(phdc); } }
/// <summary>Initializes a new instance of the <see cref="BufferedPainter"/> class and begins a buffered paint operation.</summary> /// <param name="hdc">The handle of the target DC on which the buffer will be painted.</param> /// <param name="targetRectangle">Specifies the area of the target DC in which to paint.</param> /// <param name="paintParams">The paint operation parameters. This value can be <see langword="null"/>.</param> /// <param name="fmt">The format of the buffer.</param> /// <exception cref="Win32Exception">Buffered painting could not initialize.</exception> public BufferedPainter(HDC hdc, Rectangle targetRectangle, BP_PAINTPARAMS paintParams = null, BP_BUFFERFORMAT fmt = BP_BUFFERFORMAT.BPBF_TOPDOWNDIB) { RECT target = targetRectangle; hbp = BeginBufferedPaint(hdc, target, fmt, paintParams, out var phdc); if (hbp.IsInvalid) { throw new Win32Exception(); } if (!phdc.IsNull) { Graphics = Graphics.FromHdc((IntPtr)phdc); } }
protected override void WndProc(ref Message m) { base.WndProc(ref m); if (m.Msg == 0x0f) { BP_PAINTPARAMS d = new BP_PAINTPARAMS(); pb = NativeMethod.BeginBufferedPaint(hDC, ref rect, BP_BUFFERFORMAT.TopDownDIB, ref d, out phdc); int res = NativeMethod.BufferedPaintSetAlpha(hDC, ref rect, 255); IntPtr r = NativeMethod.SendMessage(hDC, 0x318, hDC.ToInt32(), 4); res = NativeMethod.BufferedPaintSetAlpha(hDC, ref rect, 255); res = NativeMethod.EndBufferedPaint(hDC, true); } }
public BufferedPaintHandle(SafeDCHandle hdc, Rectangle targetRectangle, BP_PAINTPARAMS paintParams = null, BP_BUFFERFORMAT fmt = BP_BUFFERFORMAT.BPBF_TOPDOWNDIB) : base(IntPtr.Zero, true) { RECT target = targetRectangle; IntPtr phdc; var hbp = BeginBufferedPaint(hdc, ref target, fmt, paintParams, out phdc); if (hbp == IntPtr.Zero) { throw new Win32Exception(); } if (phdc != IntPtr.Zero) { Graphics = Graphics.FromHdc(phdc); } SetHandle(hbp); }
/// <summary> /// Initializes a new instance of the <see cref="BufferedPainter"/> class and begins a buffered animation operation. The animation consists of a /// cross-fade between the contents of two buffers over a specified period of time. /// </summary> /// <param name="wnd">The window in which the animations play.</param> /// <param name="hdc">A handle of the target DC on which the buffer is animated.</param> /// <param name="targetRectangle">Specifies the area of the target DC in which to draw.</param> /// <param name="animationParams">A structure that defines the animation operation parameters. This value can be <see langword="null"/>.</param> /// <param name="paintParams">A class that defines the paint operation parameters. This value can be <see langword="null"/>.</param> /// <param name="fmt">The format of the buffer.</param> /// <exception cref="Win32Exception">Buffered animation could not initialize.</exception> public BufferedAnimationPainter(IWin32Window wnd, SafeDCHandle hdc, Rectangle targetRectangle, BP_ANIMATIONPARAMS?animationParams = null, BP_PAINTPARAMS paintParams = null, BP_BUFFERFORMAT fmt = BP_BUFFERFORMAT.BPBF_TOPDOWNDIB) { RECT rc = targetRectangle; var ap = animationParams ?? BP_ANIMATIONPARAMS.Empty; hba = BeginBufferedAnimation(new HandleRef(wnd, wnd.Handle), hdc, ref rc, fmt, paintParams, ref ap, out IntPtr hdcFrom, out IntPtr hdcTo); if (hba.IsInvalid) { throw new Win32Exception(); } if (hdcFrom != IntPtr.Zero) { SourceGraphics = Graphics.FromHdc(hdcFrom); } if (hdcTo != IntPtr.Zero) { DestinationGraphics = Graphics.FromHdc(hdcTo); } }
/// <summary> /// Initializes a new instance of the <see cref="BufferedPainter"/> class and begins a buffered animation operation. The animation consists of a /// cross-fade between the contents of two buffers over a specified period of time. /// </summary> /// <param name="wnd">The window in which the animations play.</param> /// <param name="hdc">A handle of the target DC on which the buffer is animated.</param> /// <param name="targetRectangle">Specifies the area of the target DC in which to draw.</param> /// <param name="animationParams">A structure that defines the animation operation parameters. This value can be <see langword="null"/>.</param> /// <param name="paintParams">A class that defines the paint operation parameters. This value can be <see langword="null"/>.</param> /// <param name="fmt">The format of the buffer.</param> /// <exception cref="Win32Exception">Buffered animation could not initialize.</exception> public BufferedAnimationPainter(IWin32Window wnd, HDC hdc, Rectangle targetRectangle, BP_ANIMATIONPARAMS?animationParams = null, BP_PAINTPARAMS paintParams = null, BP_BUFFERFORMAT fmt = BP_BUFFERFORMAT.BPBF_TOPDOWNDIB) { RECT rc = targetRectangle; var ap = animationParams ?? BP_ANIMATIONPARAMS.Empty; hba = BeginBufferedAnimation(wnd.Handle, hdc, rc, fmt, paintParams, ap, out var hdcFrom, out var hdcTo); if (hba.IsInvalid) { throw new Win32Exception(); } if (!hdcFrom.IsNull) { SourceGraphics = Graphics.FromHdc((IntPtr)hdcFrom); } if (!hdcTo.IsNull) { DestinationGraphics = Graphics.FromHdc((IntPtr)hdcTo); } }
public static extern IntPtr BeginBufferedPaint(IntPtr hdc, ref RECT rect, int dwFormat, ref BP_PAINTPARAMS pPaintParams, ref IntPtr hdc2);
public static extern IntPtr BeginBufferedPaint(IntPtr hdcTarget, [In] ref Rectangle prcTarget, BP_BUFFERFORMAT dwFormat, [In] ref BP_PAINTPARAMS pPaintParams, out IntPtr phdc);
public static extern SafeBufferedPaintHandle BeginBufferedPaint(SafeDCHandle hdcTarget, [In] ref RECT prcTarget, BP_BUFFERFORMAT dwFormat, [In] BP_PAINTPARAMS pPaintParams, out IntPtr phdc);
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);
/// <summary> /// Creates a handle to a PARGB32 bitmap from an Icon. /// </summary> /// <param name="iconHandle">The handle to the icon.</param> /// <param name="iconSize">The iconSize of the icon.</param> /// <returns>A PARGB32 bitmap with the contents of the icon, including transparency.</returns> public static IntPtr CreatePARGB32HBitmap(IntPtr iconHandle, Size iconSize) { // Make sure we're ready for buffered painting. // TODO: Really we only need to do this once per thread, so an improvement // might be to have a manager create this as required per thread. Uxtheme.BufferedPaintInit(); // Create a compatible device context to work with. var deviceContextHandle = Gdi32.CreateCompatibleDC(IntPtr.Zero); if (deviceContextHandle == IntPtr.Zero) { return(IntPtr.Zero); } // Now create a 32 bit bitmap of the appropriate size, getting it's bits and handle. IntPtr bits; IntPtr hBitmap; if (!Create32BitHBITMAP(deviceContextHandle, iconSize, out bits, out hBitmap)) { Gdi32.DeleteDC(deviceContextHandle); return(IntPtr.Zero); } // Select the bitmap, keeping track of the old one. If this fails, // delete the device context and return a null handle. var oldBitmapHandle = Gdi32.SelectObject(deviceContextHandle, hBitmap); if (oldBitmapHandle == IntPtr.Zero) { Gdi32.DeleteDC(deviceContextHandle); return(IntPtr.Zero); } // Create paint params that represent our alpha blending. var bfAlpha = new BLENDFUNCTION { BlendOp = AC_SRC_OVER, BlendFlags = 0, SourceConstantAlpha = 255, AlphaFormat = AC_SRC_ALPHA }; var paintParams = new BP_PAINTPARAMS(); paintParams.cbSize = (uint)Marshal.SizeOf(paintParams); paintParams.dwFlags = BPPF_ERASE; paintParams.pBlendFunction = Marshal.AllocHGlobal(Marshal.SizeOf(bfAlpha)); Marshal.StructureToPtr(bfAlpha, paintParams.pBlendFunction, false); // Create the pointer that'll hold the device context to the buffer, set the icon rectangle. IntPtr bufferDeviceContextHandle; var iconRect = new RECT(0, 0, iconSize.Width, iconSize.Height); // Create a paint buffer handle. var paintBufferHandle = Uxtheme.BeginBufferedPaint(deviceContextHandle, ref iconRect, BP_BUFFERFORMAT.BPBF_DIB, ref paintParams, out bufferDeviceContextHandle); // Free the memory we allocated for the blend function. Marshal.FreeHGlobal(paintParams.pBlendFunction); // If we created a paint buffer successfully, we can draw the icon into it. if (paintBufferHandle != IntPtr.Zero) { // Try and draw the icon. if (Gdi32.DrawIconEx(bufferDeviceContextHandle, 0, 0, iconHandle, iconSize.Width, iconSize.Height, 0, IntPtr.Zero, (int)DI_NORMAL)) { // Now convert the buffer we've painted into PARGB32, meaning we'll end up // with a PARGB32 bitmap. ConvertBufferToPARGB32(paintBufferHandle, deviceContextHandle, iconHandle, iconSize); } // This will write the buffer contents to the destination bitmap. Uxtheme.EndBufferedPaint(paintBufferHandle, true); } // Select the old bitmpa and delete the device context. Gdi32.SelectObject(deviceContextHandle, oldBitmapHandle); Gdi32.DeleteDC(deviceContextHandle); // We're done with buffered painting. Uxtheme.BufferedPaintUnInit(); // Baddabing-baddabom, PARGB32. return(hBitmap); }