C#实现按钮透明,窗体透明的方法时间:2022-03-08 19:28:19例子一:实现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); }