1 Bitmap CaptureCursor(ref Point position)
{ CURSORINFO cursorInfo = new CURSORINFO(); cursorInfo.cbSize = Marshal.SizeOf(cursorInfo); if (!GetCursorInfo(out cursorInfo))

return null; if (cursorInfo.flags != CURSOR_SHOWING) return null; IntPtr hicon = CopyIcon(cursorInfo.hCursor); if (hicon == IntPtr.Zero)

return null; ICONINFO iconInfo; if (!GetIconInfo(hicon, out iconInfo)) return null;
position.X = cursorInfo.ptScreenPos.x - iconInfo.xHotspot; position.Y = cursorInfo.ptScreenPos.y - iconInfo.yHotspot;
using (Bitmap maskBitmap = Bitmap.FromHbitmap(iconInfo.hbmMask)) {


if (maskBitmap.Height == maskBitmap.Width * 2) { Bitmap cursor = new Bitmap(32, 32, PixelFormat.Format32bppArgb);
Color BLACK = Color.FromArgb(255, 0, 0, 0);

//cannot compare Color.Black because of different names
Color WHITE = Color.FromArgb(255, 255, 255, 255);

//cannot compare Color.White because of different names
for (int y = 0; y < 32; y++) { for (int x = 0; x < 32; x++) { Color maskPixel = maskBitmap.GetPixel(x, y);
Color cursorPixel = maskBitmap.GetPixel(x, y + 32);
if (maskPixel == WHITE && cursorPixel == BLACK) { cursor.SetPixel(x, y, Color.Transparent); }
else if (maskPixel == BLACK) { cursor.SetPixel(x, y, cursorPixel); }
else { cursor.SetPixel(x, y, cursorPixel == BLACK ? WHITE : BLACK); } } } return cursor; } }
Icon icon = Icon.FromHandle(hicon); return icon.ToBitmap(); }

 

posted on 2018-12-19 09:22  eristjy  阅读(171)  评论(0编辑  收藏  举报