/// <summary> /// Checks if the cursor has changed sice the last invokation and returns the serialized new cursor icon /// </summary> /// <returns>Serialized data bytes if icon has changed else null</returns> public static IntPtr CheckCursorChanged() { USER32.CURSORINFO ci = new USER32.CURSORINFO(); ci.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(ci); USER32.GetCursorInfo(out ci); int val = ci.hCursor.ToInt32(); if (val != ScreenSnap._previousCursor) { ScreenSnap._previousCursor = val; //return CaptureCursor(); return ci.hCursor; } return IntPtr.Zero; }
/// <summary> /// Draws cursor onto the bitmap /// </summary> /// <param name="bmp">Source bitmap</param> /// <returns>Bitmap object</returns> public static Bitmap PlaceCursor(Bitmap bmp) { Graphics g = Graphics.FromImage(bmp); { USER32.CURSORINFO ci = new USER32.CURSORINFO(); ci.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(ci); USER32.GetCursorInfo(out ci); USER32.ICONINFO icInfo; if (ci.flags == USER32.CURSORINFOFlags.CURSOR_SHOWING && ci.hCursor.ToInt32() != 0) { IntPtr hicon = USER32.CopyIcon(ci.hCursor); int x = System.Windows.Forms.Cursor.Position.X; int y = System.Windows.Forms.Cursor.Position.Y; if (USER32.GetIconInfo(hicon, out icInfo)) { x = ci.ptScreenPos.X - ((int)icInfo.xHotspot); y = ci.ptScreenPos.Y - ((int)icInfo.yHotspot); } try { //ic.ToBitmap(); //g.DrawImage(ic.ToBitmap(), System.Windows.Forms.Cursor.Position.X, System.Windows.Forms.Cursor.Position.Y); g.DrawIcon(Icon.FromHandle(ci.hCursor), x, y); USER32.DestroyIcon(hicon); } catch(Exception){} } return bmp; } }
static Icon CaptureCursor() { USER32.CURSORINFO cursorInfo = new USER32.CURSORINFO(); cursorInfo.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(cursorInfo); if (!USER32.GetCursorInfo(out cursorInfo)) return null; if (cursorInfo.flags != USER32.CURSORINFOFlags.CURSOR_SHOWING) return null; IntPtr hicon = USER32.CopyIcon(cursorInfo.hCursor); if (hicon == IntPtr.Zero) return null; USER32.ICONINFO iconInfo; if (!USER32.GetIconInfo(hicon, out iconInfo)) return null; //x = cursorInfo.ptScreenPos.X - ((int)iconInfo.xHotspot); //y = cursorInfo.ptScreenPos.Y - ((int)iconInfo.yHotspot); using (Bitmap maskBitmap = Bitmap.FromHbitmap(iconInfo.hbmMask)) { // Is this a monochrome cursor? if (maskBitmap.Height == maskBitmap.Width * 2) { Bitmap resultBitmap = new Bitmap(maskBitmap.Width, maskBitmap.Width); Graphics desktopGraphics = Graphics.FromHwnd(USER32.GetDesktopWindow()); IntPtr desktopHdc = desktopGraphics.GetHdc(); IntPtr maskHdc = GDI32.CreateCompatibleDC(desktopHdc); IntPtr oldPtr = GDI32.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. GDI32.BitBlt(resultHdc, 0, 0, 32, 32, maskHdc, 0, 32, TernaryRasterOperations.SRCCOPY); GDI32.BitBlt(resultHdc, 0, 0, 32, 32, maskHdc, 0, 0, TernaryRasterOperations.SRCINVERT); resultGraphics.ReleaseHdc(resultHdc); } IntPtr newPtr = GDI32.SelectObject(maskHdc, oldPtr); USER32.DeleteObject(newPtr); GDI32.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); return Icon.FromHandle(resultBitmap.GetHicon()); } } return Icon.FromHandle(hicon); }