public void SetBitmap(Bitmap bitmap, byte opacity) { if (!haveHandle) { return; } if (bitmap.PixelFormat != PixelFormat.Format32bppArgb) { throw new ApplicationException("位图必须是32位包含alpha 通道"); } IntPtr screenDc = Win32.GetDC(IntPtr.Zero); IntPtr memDc = Win32.CreateCompatibleDC(screenDc); IntPtr hBitmap = IntPtr.Zero; IntPtr oldBitmap = IntPtr.Zero; try { hBitmap = bitmap.GetHbitmap(Color.FromArgb(0)); // 创建GDI位图句柄,效率较低 oldBitmap = Win32.SelectObject(memDc, hBitmap); Interop.Size size = new Interop.Size(bitmap.Width, bitmap.Height); Interop.Point pointSource = new Interop.Point(0, 0); Interop.Point topPos = new Interop.Point(Left, Top); Interop.BLENDFUNCTION blend = new Interop.BLENDFUNCTION(); blend.BlendOp = Win32.AC_SRC_OVER; blend.BlendFlags = 0; blend.SourceConstantAlpha = opacity; blend.AlphaFormat = Win32.AC_SRC_ALPHA; Win32.UpdateLayeredWindow(Handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, Win32.ULW_ALPHA); } finally { Win32.ReleaseDC(IntPtr.Zero, screenDc); if (hBitmap != IntPtr.Zero) { Win32.SelectObject(memDc, oldBitmap); Win32.DeleteObject(hBitmap); } Win32.DeleteDC(memDc); } }