C# WinForm 无锯齿透明异形窗体
通过UpdateLayeredWindow和背景图创建异形窗体
重载CreateParams
private const int WS_EX_LAYERED = 0x00080000; protected override CreateParams CreateParams { get { CreateParams cp = base.CreateParams; cp.ExStyle |= WS_EX_LAYERED; return cp; } }
设置图片和透明度
public void SetBitmap(Bitmap bitmap, byte opacity) { if (bitmap.PixelFormat != PixelFormat.Format32bppArgb) throw new ApplicationException("位图必须是32位包含alpha 通道"); IntPtr screenDc = APIHelper.GetDC(IntPtr.Zero); IntPtr memDc = APIHelper.CreateCompatibleDC(screenDc); IntPtr hBitmap = IntPtr.Zero; IntPtr oldBitmap = IntPtr.Zero; try { hBitmap = bitmap.GetHbitmap(Color.FromArgb(0)); oldBitmap = APIHelper.SelectObject(memDc, hBitmap); APIHelper.Size size = new APIHelper.Size(bitmap.Width, bitmap.Height); APIHelper.Point pointSource = new APIHelper.Point(0, 0); APIHelper.Point topPos = new APIHelper.Point(Left, Top); APIHelper.BLENDFUNCTION blend = new APIHelper.BLENDFUNCTION(); blend.BlendOp = APIHelper.AC_SRC_OVER; blend.BlendFlags = 0; blend.SourceConstantAlpha = opacity; blend.AlphaFormat = APIHelper.AC_SRC_ALPHA; APIHelper.UpdateLayeredWindow(Handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, APIHelper.ULW_ALPHA); } finally { APIHelper.ReleaseDC(IntPtr.Zero, screenDc); if (hBitmap != IntPtr.Zero) { APIHelper.SelectObject(memDc, oldBitmap); APIHelper.DeleteObject(hBitmap); } APIHelper.DeleteDC(memDc); } }
APIHelper :
[StructLayout(LayoutKind.Sequential)] public struct Size { public Int32 cx; public Int32 cy; public Size(Int32 x, Int32 y) { cx = x; cy = y; } } [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct BLENDFUNCTION { public byte BlendOp; public byte BlendFlags; public byte SourceConstantAlpha; public byte AlphaFormat; } [StructLayout(LayoutKind.Sequential)] public struct Point { public Int32 x; public Int32 y; public Point(Int32 x, Int32 y) { this.x = x; this.y = y; } } public const byte AC_SRC_OVER = 0; public const Int32 ULW_ALPHA = 2; public const byte AC_SRC_ALPHA = 1; [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)] public static extern IntPtr CreateCompatibleDC(IntPtr hDC); [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)] public static extern IntPtr GetDC(IntPtr hWnd); [DllImport("gdi32.dll", ExactSpelling = true)] public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObj); [DllImport("user32.dll", ExactSpelling = true)] public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC); [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)] public static extern int DeleteDC(IntPtr hDC); [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)] public static extern int DeleteObject(IntPtr hObj); [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)] public static extern int UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, ref Point pptDst, ref Size psize, IntPtr hdcSrc, ref Point pptSrc, Int32 crKey, ref BLENDFUNCTION pblend, Int32 dwFlags); [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)] public static extern IntPtr ExtCreateRegion(IntPtr lpXform, uint nCount, IntPtr rgnData);
使用示例
public ShapeForm() { InitializeComponent(); var bitmap = Properties.Resources.heart; SetBitmap(bitmap ,125); }
结果展示: 半透明 心形窗体
posted on 2019-05-24 16:05 K丶Formular 阅读(917) 评论(0) 编辑 收藏 举报