コード例 #1
0
        /// <summary>
        /// This method will capture the current Cursor by using User32 Code
        /// </summary>
        /// <returns>A IElement with the Mouse Cursor as Image in it.</returns>
        public static bool TryGetCurrentCursor(out BitmapSource bitmapSource, out NativePoint location)
        {
            bitmapSource = null;
            location     = NativePoint.Empty;
            var cursorInfo = CursorInfo.Create();

            if (!NativeCursorMethods.GetCursorInfo(ref cursorInfo))
            {
                return(false);
            }

            if (cursorInfo.Flags != CursorInfoFlags.Showing)
            {
                return(false);
            }

            using (var safeIcon = NativeIconMethods.CopyIcon(cursorInfo.CursorHandle))
            {
                if (!NativeIconMethods.GetIconInfo(safeIcon, out var iconInfo))
                {
                    return(false);
                }

                using (iconInfo.BitmaskBitmapHandle)
                    using (iconInfo.ColorBitmapHandle)
                    {
                        var cursorLocation = User32Api.GetCursorLocation();
                        bitmapSource = Imaging.CreateBitmapSourceFromHIcon(safeIcon.DangerousGetHandle(), Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
                        location     = new NativePoint(cursorLocation.X - iconInfo.Hotspot.X, cursorLocation.Y - iconInfo.Hotspot.Y);
                    }
            }

            return(true);
        }
コード例 #2
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();
            }

            var cursorInfo = CursorInfo.Create();

            if (!NativeCursorMethods.GetCursorInfo(ref cursorInfo))
            {
                return(capture);
            }
            if (cursorInfo.Flags != CursorInfoFlags.Showing)
            {
                return(capture);
            }
            using (var safeIcon = NativeIconMethods.CopyIcon(cursorInfo.CursorHandle))
            {
                if (!NativeIconMethods.GetIconInfo(safeIcon, out var iconInfo))
                {
                    return(capture);
                }
                using (iconInfo.BitmaskBitmapHandle)
                    using (iconInfo.ColorBitmapHandle)
                    {
                        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;
                        }
                    }
            }
            return(capture);
        }