public void SelectBitmap(Bitmap bitmap, int opacity) { if (bitmap.PixelFormat != PixelFormat.Format32bppArgb) { throw new ApplicationException("The bitmap must be 32bpp with alpha-channel."); } IntPtr screenDc = NativeMethods.GetDC(IntPtr.Zero); IntPtr memDc = NativeMethods.CreateCompatibleDC(screenDc); IntPtr hBitmap = IntPtr.Zero; IntPtr hOldBitmap = IntPtr.Zero; try { hBitmap = bitmap.GetHbitmap(Color.FromArgb(0)); hOldBitmap = NativeMethods.SelectObject(memDc, hBitmap); SIZE newSize = new SIZE(bitmap.Width, bitmap.Height); POINT sourceLocation = new POINT(0, 0); POINT newLocation = new POINT(this.Left, this.Top); BLENDFUNCTION blend = new BLENDFUNCTION(); blend.BlendOp = NativeMethods.AC_SRC_OVER; blend.BlendFlags = 0; blend.SourceConstantAlpha = (byte)opacity; blend.AlphaFormat = NativeMethods.AC_SRC_ALPHA; NativeMethods.UpdateLayeredWindow(this.Handle, screenDc, ref newLocation, ref newSize, memDc, ref sourceLocation, 0, ref blend, NativeMethods.ULW_ALPHA); } finally { NativeMethods.ReleaseDC(IntPtr.Zero, screenDc); if (hBitmap != IntPtr.Zero) { NativeMethods.SelectObject(memDc, hOldBitmap); NativeMethods.DeleteObject(hBitmap); } NativeMethods.DeleteDC(memDc); } }
public static MyCursor CaptureCursor() { CursorInfo cursorInfo = new CursorInfo(); cursorInfo.cbSize = Marshal.SizeOf(cursorInfo); if (GetCursorInfo(out cursorInfo) && cursorInfo.flags == CURSOR_SHOWING) { cursorInfo.ptScreenPos = CaptureHelpers.GetZeroBasedMousePosition(); IntPtr hicon = CopyIcon(cursorInfo.hCursor); if (hicon != IntPtr.Zero) { IconInfo iconInfo; if (GetIconInfo(hicon, out iconInfo)) { Point position = new Point(cursorInfo.ptScreenPos.X - iconInfo.xHotspot, cursorInfo.ptScreenPos.Y - iconInfo.yHotspot); using (Bitmap maskBitmap = Bitmap.FromHbitmap(iconInfo.hbmMask)) { Bitmap resultBitmap = null; if (IsCursorMonochrome(maskBitmap)) { resultBitmap = new Bitmap(maskBitmap.Width, maskBitmap.Width); Graphics desktopGraphics = Graphics.FromHwnd(GetDesktopWindow()); IntPtr desktopHdc = desktopGraphics.GetHdc(); IntPtr maskHdc = NativeMethods.CreateCompatibleDC(desktopHdc); IntPtr oldPtr = NativeMethods.SelectObject(maskHdc, maskBitmap.GetHbitmap()); using (Graphics resultGraphics = Graphics.FromImage(resultBitmap)) { IntPtr resultHdc = resultGraphics.GetHdc(); // These two operation will result in a black cursor over a white background. // Later in the code, a call to MakeTransparent() will get rid of the white background. NativeMethods.BitBlt(resultHdc, 0, 0, 32, 32, maskHdc, 0, 32, CopyPixelOperation.SourceCopy); NativeMethods.BitBlt(resultHdc, 0, 0, 32, 32, maskHdc, 0, 0, CopyPixelOperation.SourceInvert); resultGraphics.ReleaseHdc(resultHdc); } IntPtr newPtr = NativeMethods.SelectObject(maskHdc, oldPtr); NativeMethods.DeleteDC(newPtr); NativeMethods.DeleteDC(maskHdc); desktopGraphics.ReleaseHdc(desktopHdc); // Remove the white background from the BitBlt calls, // resulting in a black cursor over a transparent background. resultBitmap.MakeTransparent(Color.White); } else { resultBitmap = Icon.FromHandle(hicon).ToBitmap(); } return(new MyCursor(new Cursor(cursorInfo.hCursor), position, resultBitmap)); } } } } return(new MyCursor()); }