您的位置:首页 > 编程语言 > C#

C#实现按钮透明,窗体透明的方法

2010-08-31 14:45 525 查看
例子一:实现PictureBox的透明化。

1.1 调用WinAPI的类



public class Pink
    {
        public static void DrawAlpha(Graphics gxBuffer, Image barImage, Rectangle barRect, byte transp)
        {
            using (Graphics gxSrc = Graphics.FromImage(barImage))
            {
                IntPtr hdcDst = gxBuffer.GetHdc();
                IntPtr hdcSrc = gxSrc.GetHdc();
                BlendFunction blendFunction = new BlendFunction();
                blendFunction.BlendOp = (byte)0;
                blendFunction.BlendFlags = (byte)0;
                blendFunction.SourceConstantAlpha = transp;
                blendFunction.AlphaFormat = (byte)0;
                AlphaBlend(hdcDst, barRect.Left, barRect.Top, barRect.Size.Width, barRect.Size.Height, hdcSrc, 0, 0, barImage.Width, barImage.Height, blendFunction);
                gxBuffer.ReleaseHdc(hdcDst);
                gxSrc.ReleaseHdc(hdcSrc);
            }
        }

        [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);
    
    }




1.2在界面上重画PictrueBox(需要设置PictureBox为隐藏)



private void picboxZoomIn_Paint(object sender, PaintEventArgs e)
        {
            Bitmap bitmap = new Bitmap(@"/Program Files/Collecter/pic/zoomin.png");
            Image image = Image.FromHbitmap(bitmap.GetHbitmap());
            Rectangle rect=new Rectangle(0,0,30,30);
            Pink.DrawAlpha(e.Graphics, image, rect, 20);
            //e.Graphics.DrawImage(image, 30, 30);
        }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: