Пример #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()))
     {
         User32Api.GetIconInfo(iconHandle, out var iconInfo);
         iconInfo.Hotspot = new NativePoint(hotspotX, hotspotY);
         iconInfo.IsIcon  = false;
         var icon = User32Api.CreateIconIndirect(ref iconInfo);
         return(new Cursor(icon));
     }
 }
Пример #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();
            }
            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);
        }