Beispiel #1
0
            private void SetBitmap(Bitmap bitmap, byte opacity)
            {
                if (bitmap.PixelFormat != PixelFormat.Format32bppArgb)
                {
                    throw new ApplicationException("The bitmap must be 32ppp with alpha-channel.");
                }

                var screenDc  = WinApi.GetDC(IntPtr.Zero);
                var memDc     = WinApi.CreateCompatibleDC(screenDc);
                var hBitmap   = IntPtr.Zero;
                var oldBitmap = IntPtr.Zero;

                try
                {
                    hBitmap   = bitmap.GetHbitmap(Color.FromArgb(0));
                    oldBitmap = WinApi.SelectObject(memDc, hBitmap);

                    var size        = new WinApi.SIZE(bitmap.Width, bitmap.Height);
                    var pointSource = new WinApi.POINT(0, 0);
                    var topPos      = new WinApi.POINT(Left, Top);
                    var blend       = new WinApi.BLENDFUNCTION
                    {
                        BlendOp             = WinApi.AC_SRC_OVER,
                        BlendFlags          = 0,
                        SourceConstantAlpha = opacity,
                        AlphaFormat         = WinApi.AC_SRC_ALPHA
                    };

                    WinApi.UpdateLayeredWindow(Handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0,
                                               ref blend, WinApi.ULW_ALPHA);
                }
                finally
                {
                    WinApi.ReleaseDC(IntPtr.Zero, screenDc);
                    if (hBitmap != IntPtr.Zero)
                    {
                        WinApi.SelectObject(memDc, oldBitmap);
                        WinApi.DeleteObject(hBitmap);
                    }

                    WinApi.DeleteDC(memDc);
                }
            }
Beispiel #2
0
        private void Render()
        {
            var screenDc = WinApi.GetDC(WinApi.NullHandleRef);

            if (screenDc == IntPtr.Zero)
            {
                return;
            }
            try {
                var memDc = WinApi.CreateCompatibleDC(new HandleRef(null, screenDc));
                if (memDc == IntPtr.Zero)
                {
                    return;
                }
                try {
                    using (Bitmap bmp = GetWindowBitmap(Size.Width, Size.Height)) {
                        IntPtr hBitmap    = bmp.GetHbitmap(_transparent);
                        IntPtr hOldBitmap = WinApi.SelectObject(memDc, hBitmap);

                        WinApi.POINT newLocation = new WinApi.POINT(Location);
                        WinApi.SIZE  newSize     = new WinApi.SIZE(Size);
                        WinApi.UpdateLayeredWindow(Handle, screenDc, ref newLocation, ref newSize, memDc, ref _ptZero, 0, ref _blend, WinApi.BlendingFlags.ULW_ALPHA);

                        if (hBitmap != IntPtr.Zero)
                        {
                            WinApi.SelectObject(memDc, hOldBitmap);
                            WinApi.DeleteObject(hBitmap);
                        }
                    }
                } finally {
                    WinApi.DeleteDC(new HandleRef(null, memDc));
                }
            } finally {
                WinApi.ReleaseDC(WinApi.NullHandleRef, new HandleRef(null, screenDc));
            }
        }