Esempio n. 1
0
        /// <summary>
        ///     This method will capture the current Cursor by using User32 Code
        /// </summary>
        /// <returns>A Capture Object with the Mouse Cursor information in it.</returns>
        public static ICapture CaptureCursor(ICapture capture)
        {
            Log.Debug().WriteLine("Capturing the mouse cursor.");
            if (capture == null)
            {
                capture = new Capture();
            }
            if (!User32Api.GetCursorInfo(out var cursorInfo))
            {
                return(capture);
            }
            if (cursorInfo.Flags != CursorInfoFlags.Showing)
            {
                return(capture);
            }
            using (var safeIcon = User32Api.CopyIcon(cursorInfo.CursorHandle))
            {
                if (!User32Api.GetIconInfo(safeIcon, out var iconInfo))
                {
                    return(capture);
                }
                var cursorLocation = User32Api.GetCursorLocation();
                // Allign cursor location to Bitmap coordinates (instead of Screen coordinates)
                var x = cursorLocation.X - iconInfo.Hotspot.X - capture.ScreenBounds.X;
                var y = cursorLocation.Y - iconInfo.Hotspot.Y - capture.ScreenBounds.Y;
                // Set the location
                capture.CursorLocation = new NativePoint(x, y);

                using (var icon = Icon.FromHandle(safeIcon.DangerousGetHandle()))
                {
                    capture.Cursor = icon;
                }

                if (iconInfo.BitmaskBitmapHandle != IntPtr.Zero)
                {
                    DeleteObject(iconInfo.BitmaskBitmapHandle);
                }
                if (iconInfo.ColorBitmapHandle != IntPtr.Zero)
                {
                    DeleteObject(iconInfo.ColorBitmapHandle);
                }
            }
            return(capture);
        }