public static extern BOOL UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, ref POINT pptDst, ref SIZE psize, IntPtr hdcSrc, ref POINT pprSrc, Int32 crKey, ref GDI32.BLENDFUNCTION pblend, Int32 dwFlags);
/// <summary> /// Redraws the bitmap overlay. /// </summary> /// <returns>True if successful, false otherwise.</returns> protected bool RefreshBitmap() { // The bitmap must be 32-bit RGBA if (_bitmap == null || _bitmap.PixelFormat != PixelFormat.Format32bppArgb) return false; // Create a memory DC that's compatible with the screen IntPtr screenDC = Win32.GetDC(IntPtr.Zero); if (screenDC == IntPtr.Zero) return false; IntPtr memDC = GDI32.CreateCompatibleDC(screenDC); if (memDC == IntPtr.Zero) { Win32.ReleaseDC(IntPtr.Zero, screenDC); return false; } // Prepare to draw the bitmap IntPtr hBitmap = IntPtr.Zero; IntPtr oldBitmap = IntPtr.Zero; bool success = false; try { // Select the bitmap into the memory DC hBitmap = _bitmap.GetHbitmap(Color.FromArgb(0)); // grab a GDI handle from this GDI+ bitmap oldBitmap = GDI32.SelectObject(memDC, hBitmap); // Call UpdateLayeredWindow to effectively blit the contents of the memory DC into the form while performing alpha blending Win32.POINT windowTopLeft = new Win32.POINT(Left, Top); Win32.SIZE bitmapSize = new Win32.SIZE(_bitmap.Width, _bitmap.Height); Win32.POINT bitmapTopLeft = new Win32.POINT(0, 0); byte blendAlpha = 0; if (_bitmapOpacity < 0) blendAlpha = 0; else if (_bitmapOpacity > 1) blendAlpha = 255; else blendAlpha = (byte)(_bitmapOpacity * 255); GDI32.BLENDFUNCTION blendFunc = new GDI32.BLENDFUNCTION(); blendFunc.BlendOp = GDI32.AC_SRC_OVER; blendFunc.BlendFlags = 0; blendFunc.SourceConstantAlpha = blendAlpha; blendFunc.AlphaFormat = GDI32.AC_SRC_ALPHA; Win32.UpdateLayeredWindow(Handle, screenDC, ref windowTopLeft, ref bitmapSize, memDC, ref bitmapTopLeft, 0, ref blendFunc, Win32.ULW_ALPHA); success = true; } finally { // Clean up the resources if (hBitmap != IntPtr.Zero) { GDI32.SelectObject(memDC, oldBitmap); GDI32.DeleteObject(hBitmap); } GDI32.DeleteDC(memDC); Win32.ReleaseDC(IntPtr.Zero, screenDC); } return success; }
/// <summary> /// Redraws the bitmap overlay. /// </summary> /// <returns>True if successful, false otherwise.</returns> protected bool RefreshBitmap() { // The bitmap must be 32-bit RGBA if (_bitmap == null || _bitmap.PixelFormat != PixelFormat.Format32bppArgb) { return(false); } // Create a memory DC that's compatible with the screen IntPtr screenDC = Win32.GetDC(IntPtr.Zero); if (screenDC == IntPtr.Zero) { return(false); } IntPtr memDC = GDI32.CreateCompatibleDC(screenDC); if (memDC == IntPtr.Zero) { Win32.ReleaseDC(IntPtr.Zero, screenDC); return(false); } // Prepare to draw the bitmap IntPtr hBitmap = IntPtr.Zero; IntPtr oldBitmap = IntPtr.Zero; bool success = false; try { // Select the bitmap into the memory DC hBitmap = _bitmap.GetHbitmap(Color.FromArgb(0)); // grab a GDI handle from this GDI+ bitmap oldBitmap = GDI32.SelectObject(memDC, hBitmap); // Call UpdateLayeredWindow to effectively blit the contents of the memory DC into the form while performing alpha blending Win32.POINT windowTopLeft = new Win32.POINT(Left, Top); Win32.SIZE bitmapSize = new Win32.SIZE(_bitmap.Width, _bitmap.Height); Win32.POINT bitmapTopLeft = new Win32.POINT(0, 0); byte blendAlpha = 0; if (_bitmapOpacity < 0) { blendAlpha = 0; } else if (_bitmapOpacity > 1) { blendAlpha = 255; } else { blendAlpha = (byte)(_bitmapOpacity * 255); } GDI32.BLENDFUNCTION blendFunc = new GDI32.BLENDFUNCTION(); blendFunc.BlendOp = GDI32.AC_SRC_OVER; blendFunc.BlendFlags = 0; blendFunc.SourceConstantAlpha = blendAlpha; blendFunc.AlphaFormat = GDI32.AC_SRC_ALPHA; Win32.UpdateLayeredWindow(Handle, screenDC, ref windowTopLeft, ref bitmapSize, memDC, ref bitmapTopLeft, 0, ref blendFunc, Win32.ULW_ALPHA); success = true; } finally { // Clean up the resources if (hBitmap != IntPtr.Zero) { GDI32.SelectObject(memDC, oldBitmap); GDI32.DeleteObject(hBitmap); } GDI32.DeleteDC(memDC); Win32.ReleaseDC(IntPtr.Zero, screenDC); } return(success); }
public static extern bool UpdateLayeredWindow(IntPtr hWnd, IntPtr hdcDst, ref Point pptDst, ref Size psize, IntPtr hdcSrc, ref Point pptSrc, int crKey, ref GDI32.BLENDFUNCTION pblend, int dwFlags);