コード例 #1
0
        public void SetImageUpdateLayeredWindow(Bitmap image, int newOpacity = 255)
        {
            if (image == null)
            {
                return;
            }

            if (image.PixelFormat != PixelFormat.Format32bppArgb)
            {
                throw new ApplicationException("The bitmap must be 32ppp with alpha-channel.");
            }

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

            try
            {
                hBitmap = image.GetHbitmap(Color.FromArgb(0));

                oldBitmap = FormStyleAPI.SelectObject(memDc, hBitmap);

                var size = new FormStyleAPI.Size {
                    cx = image.Width, cy = image.Height
                };

                var pointSource = new FormStyleAPI.Point {
                    x = 0, y = 0
                };

                var topPos = new FormStyleAPI.Point {
                    x = Left, y = Top
                };

                var blend = new FormStyleAPI.BLENDFUNCTION
                {
                    BlendOp             = FormStyleAPI.AC_SRC_OVER,
                    BlendFlags          = 0,
                    SourceConstantAlpha = (byte)newOpacity,
                    AlphaFormat         = FormStyleAPI.AC_SRC_ALPHA
                };

                FormStyleAPI.UpdateLayeredWindow(Handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, FormStyleAPI.ULW_ALPHA);
            }
            finally
            {
                FormStyleAPI.ReleaseDC(IntPtr.Zero, screenDc);

                if (hBitmap != IntPtr.Zero)
                {
                    FormStyleAPI.SelectObject(memDc, oldBitmap);
                    FormStyleAPI.DeleteObject(hBitmap);
                }
                FormStyleAPI.DeleteDC(memDc);
            }
        }
コード例 #2
0
ファイル: IrregularFormSkin.cs プロジェクト: zyj0021/Fork
        public void SetBits()
        {
            if (BackgroundImage != null)
            {
                //绘制绘图层背景
                Bitmap bitmap = new Bitmap(BackgroundImage, base.Width, base.Height);
                if (!Image.IsCanonicalPixelFormat(bitmap.PixelFormat) || !Image.IsAlphaPixelFormat(bitmap.PixelFormat))
                {
                    throw new ApplicationException("图片必须是32位带Alhpa通道的图片。");
                }
                IntPtr oldBits  = IntPtr.Zero;
                IntPtr screenDC = FormStyleAPI.GetDC(IntPtr.Zero);
                IntPtr hBitmap  = IntPtr.Zero;
                IntPtr memDc    = FormStyleAPI.CreateCompatibleDC(screenDC);

                try
                {
                    FormStyleAPI.Point         topLoc     = new FormStyleAPI.Point(Left, Top);
                    FormStyleAPI.Size          bitMapSize = new FormStyleAPI.Size(Width, Height);
                    FormStyleAPI.BLENDFUNCTION blendFunc  = new FormStyleAPI.BLENDFUNCTION();
                    FormStyleAPI.Point         srcLoc     = new FormStyleAPI.Point(0, 0);

                    hBitmap = bitmap.GetHbitmap(Color.FromArgb(0));
                    oldBits = FormStyleAPI.SelectObject(memDc, hBitmap);

                    blendFunc.BlendOp             = FormStyleAPI.AC_SRC_OVER;
                    blendFunc.SourceConstantAlpha = Byte.Parse(((int)(Main.Opacity * 255)).ToString());
                    blendFunc.AlphaFormat         = FormStyleAPI.AC_SRC_ALPHA;
                    blendFunc.BlendFlags          = 0;

                    FormStyleAPI.UpdateLayeredWindow(Handle, screenDC, ref topLoc, ref bitMapSize, memDc, ref srcLoc, 0, ref blendFunc, FormStyleAPI.ULW_ALPHA);
                }
                finally
                {
                    if (hBitmap != IntPtr.Zero)
                    {
                        FormStyleAPI.SelectObject(memDc, oldBits);
                        FormStyleAPI.DeleteObject(hBitmap);
                    }
                    FormStyleAPI.ReleaseDC(IntPtr.Zero, screenDC);
                    FormStyleAPI.DeleteDC(memDc);
                    bitmap.Dispose();
                }
            }
        }