public static extern bool UpdateLayeredWindow( IntPtr hWnd, IntPtr hdcDst, [In] ref NativeMethods.Point pptDst, [In] ref NativeMethods.Size pSize, IntPtr hdcSrc, [In] ref NativeMethods.Point pptSrc, int crKey, [In] ref NativeMethods.BlendFunction pBlend, uint dwFlags);
private void UpdateLayeredWindowBitmap() { if (this.surfaceBuffer.IsDisposed || this.terminated) { return; } using (Graphics graphics = Graphics.FromHwnd(IntPtr.Zero)) { IntPtr hdc = graphics.GetHdc(); IntPtr hgdiobj = NativeMethods.SelectObject(this.surfaceBuffer.DeviceContext, this.surfaceBuffer.Handle); NativeMethods.BlendFunction pBlend = new NativeMethods.BlendFunction() { BlendOp = 0, BlendFlags = 0, SourceConstantAlpha = byte.MaxValue, AlphaFormat = 1 }; NativeMethods.Point point = new NativeMethods.Point(); point.X = this.Left; point.Y = this.Top; NativeMethods.Point pptDst = point; NativeMethods.Size pSize = new NativeMethods.Size() { Width = this.surfaceBuffer.Width, Height = this.surfaceBuffer.Height }; point = new NativeMethods.Point(); point.X = 0; point.Y = 0; NativeMethods.Point pptSrc = point; IntPtr handle = IntPtr.Zero; try { if (!this.terminated) { if (this.InvokeRequired) { this.Invoke((Action)(() => handle = this.Handle)); } else { handle = this.Handle; } NativeMethods.UpdateLayeredWindow(handle, hdc, ref pptDst, ref pSize, this.surfaceBuffer.DeviceContext, ref pptSrc, 0, ref pBlend, 2U); } } catch (ObjectDisposedException ex) { return; } NativeMethods.SelectObject(this.surfaceBuffer.DeviceContext, hgdiobj); graphics.ReleaseHdc(hdc); } }
private void UpdateLayeredWindowBitmap() { if (surfaceBuffer.IsDisposed || this.terminated) { return; } using (var gScreen = Graphics.FromHwnd(IntPtr.Zero)) { var hScreenDC = gScreen.GetHdc(); var hOldBitmap = NativeMethods.SelectObject(surfaceBuffer.DeviceContext, surfaceBuffer.Handle); var blend = new NativeMethods.BlendFunction { BlendOp = NativeMethods.AC_SRC_OVER, BlendFlags = 0, SourceConstantAlpha = 255, AlphaFormat = NativeMethods.AC_SRC_ALPHA }; var windowPosition = new NativeMethods.Point { X = this.Left, Y = this.Top }; var surfaceSize = new NativeMethods.Size { Width = surfaceBuffer.Width, Height = surfaceBuffer.Height }; var surfacePosition = new NativeMethods.Point { X = 0, Y = 0 }; IntPtr handle = IntPtr.Zero; try { if (!this.terminated) { if (this.InvokeRequired) { this.Invoke(new Action(() => { handle = this.Handle; })); } else { handle = this.Handle; } NativeMethods.UpdateLayeredWindow( handle, hScreenDC, ref windowPosition, ref surfaceSize, surfaceBuffer.DeviceContext, //hBitmap, ref surfacePosition, 0, ref blend, NativeMethods.ULW_ALPHA); } } catch (ObjectDisposedException) { return; } NativeMethods.SelectObject(surfaceBuffer.DeviceContext, hOldBitmap); gScreen.ReleaseHdc(hScreenDC); } }