Ejemplo n.º 1
0
        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);
            }
        }