Exemple #1
0
        private static Bitmap CaptureCursor(ref Point location)
        {
            try
            {
                WinApi.User32.CURSORINFO cursorInfo = new WinApi.User32.CURSORINFO();
                cursorInfo.cbSize = Marshal.SizeOf(cursorInfo);
                if (!WinApi.User32.GetCursorInfo(out cursorInfo)) return null;
                if (cursorInfo.flags != WinApi.User32.CURSOR_SHOWING) return null;

                IntPtr hicon = WinApi.User32.CopyIcon(cursorInfo.hCursor);
                WinApi.User32.ICONINFO icInfo;
                if (!WinApi.User32.GetIconInfo(hicon, out icInfo)) return null;

                location.X = cursorInfo.ptScreenPos.x - ((int)icInfo.xHotspot);
                location.Y = cursorInfo.ptScreenPos.y - ((int)icInfo.yHotspot);

                return Icon.FromHandle(hicon).ToBitmap();
            }
            catch
            {
                // do nothing, just return null
                location = new Point();
                return null;
            }
        }
Exemple #2
0
        private static Bitmap CaptureCursor(ref Point location)
        {
            try
            {
                WinApi.User32.CURSORINFO cursorInfo = new WinApi.User32.CURSORINFO();
                cursorInfo.cbSize = Marshal.SizeOf(cursorInfo);
                if (!WinApi.User32.GetCursorInfo(out cursorInfo))
                {
                    return(null);
                }
                if (cursorInfo.flags != WinApi.User32.CURSOR_SHOWING)
                {
                    return(null);
                }

                IntPtr hicon = WinApi.User32.CopyIcon(cursorInfo.hCursor);
                WinApi.User32.ICONINFO icInfo;
                if (!WinApi.User32.GetIconInfo(hicon, out icInfo))
                {
                    return(null);
                }

                location.X = cursorInfo.ptScreenPos.x - ((int)icInfo.xHotspot);
                location.Y = cursorInfo.ptScreenPos.y - ((int)icInfo.yHotspot);

                return(Icon.FromHandle(hicon).ToBitmap());
            }
            catch
            {
                // do nothing, just return null
                location = new Point();
                return(null);
            }
        }