半透明效果的实现

    public struct BlendFunction
    {
        public byte BlendOp;
        public byte BlendFlags;
        public byte SourceConstantAlpha;
        public byte AlphaFormat;
    }

    public enum BlendOperation : byte
    {
        AC_SRC_OVER = 0x00
    }

    public enum BlendFlags : byte
    {
        Zero = 0x00
    }

    public enum SourceConstantAlpha : byte
    {
        Transparent = 0x00,
        Opaque = 0xFF
    }

    public enum AlphaFormat : byte
    {
        AC_SRC_ALPHA = 0x01
    }

 

[DllImport("coredll.dll")]
extern public static Int32 AlphaBlend(IntPtr hdcDest, Int32 xDest, Int32 yDest, Int32 cxDest, Int32 cyDest, IntPtr hdcSrc, Int32 xSrc, Int32 ySrc, Int32 cxSrc, Int32 cySrc, BlendFunction blendFunction);

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            if (backBuffer != null)
            {
                // We need a Graphics object on the buffer to get an HDC
                using (Graphics gxBuffer = Graphics.FromImage(backBuffer))
                {
                    // Since we nop'd OnPaintBackground, take care of it here
                    gxBuffer.Clear(this.BackColor);

                    // AlphaBlend takes two HDC's - one source and one destination.  Here's the source.
                    using (Graphics gxSrc = Graphics.FromImage(displayImage))
                    {
                        IntPtr hdcDst = gxBuffer.GetHdc();
                        IntPtr hdcSrc = gxSrc.GetHdc();
 
                        BlendFunction blendFunction = new BlendFunction();
                        blendFunction.BlendOp = (byte)BlendOperation.AC_SRC_OVER;   // Only supported blend operation
                        blendFunction.BlendFlags = (byte)BlendFlags.Zero;           // Documentation says put 0 here
                        blendFunction.SourceConstantAlpha = transparencyValue;      // Constant alpha factor
                        blendFunction.AlphaFormat = (byte)0;                        // Don't look for per pixel alpha

                        int left = this.Width / 2 - (displayImage.Width / 2);   // Get x co-or based on bitmap width
                        int top = 100;                                              // y co-or
                        AlphaBlend(hdcDst, left, top,
                            displayImage.Width, displayImage.Height,
                            hdcSrc, 0, 0,
                            displayImage.Width, displayImage.Height,
                            blendFunction);
 
                        gxBuffer.ReleaseHdc(hdcDst);    // Required cleanup to GetHdc()
                        gxSrc.ReleaseHdc(hdcSrc);       // Required cleanup to GetHdc()
                    }
                }

                // Put the final composed image on screen.
                e.Graphics.DrawImage(backBuffer, 0, 0);
            }
            else
                e.Graphics.Clear(this.BackColor);
        }

阅读更多
想对作者说点什么? 我来说一句

Unity3d插件实现半透明效果

2013年04月28日 186KB 下载

实现半透明效果的菜单程序

2012年03月10日 52KB 下载

Android扇形进度条的实现

2015年11月05日 10KB 下载

CustomForm.DLL

2010年01月11日 24KB 下载

窗口半透明

2012年11月08日 468B 下载

半透明activity.zip

2014年07月10日 1.67MB 下载

半透明图片叠加的功能

2009年09月29日 927KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭