Example #1
0
 /// <summary>
 ///     Create a cursor from the supplied bitmap & hotspot coordinates
 /// </summary>
 /// <param name="bitmap">Bitmap to create an icon from</param>
 /// <param name="hotspotX">Hotspot X coordinate</param>
 /// <param name="hotspotY">Hotspot Y coordinate</param>
 /// <returns>Cursor</returns>
 private static Cursor CreateCursor(Bitmap bitmap, int hotspotX, int hotspotY)
 {
     using (var iconHandle = new SafeIconHandle(bitmap.GetHicon()))
     {
         NativeIconMethods.GetIconInfo(iconHandle, out var iconInfo);
         iconInfo.Hotspot = new NativePoint(hotspotX, hotspotY);
         iconInfo.IsIcon  = false;
         var icon = NativeIconMethods.CreateIconIndirect(ref iconInfo);
         return(new Cursor(icon));
     }
 }
Example #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);
        }