获取系统光标

我读了这个问题,发现我有一个类似的问题:

检查光标类型

这对于帮助ACS来说为时已晚,但是如果其他任何人都在寻找它,我通过结合一些资源找到了解决方案。 首先,我在codeproject上找到了一篇很好的文章:

DesktopCaptureWithMouse

这给了我获取系统鼠标图标的方式。 然后在Dreaming In Code中,我找到了一个不错的位图比较例程:

位图比较

总之,我明白了。

注意,您需要将这些添加到您的项目中:


using System.Runtime.InteropServices;
using System.Security.Cryptography; 
首先,将其添加到您的项目中

       public const Int32 CURSOR_SHOWING = 0x00000001;
        [StructLayout(LayoutKind.Sequential)]
        public struct ICONINFO
        {
            public bool fIcon;         // Specifies whether this structure defines an icon or a cursor. A value of TRUE specifies 
            public Int32 xHotspot;     // Specifies the x-coordinate of a cursor's hot spot. If this structure defines an icon, the hot 
            public Int32 yHotspot;     // Specifies the y-coordinate of the cursor's hot spot. If this structure defines an icon, the hot 
            public IntPtr hbmMask;     // (HBITMAP) Specifies the icon bitmask bitmap. If this structure defines a black and white icon, 
            public IntPtr hbmColor;    // (HBITMAP) Handle to the icon color bitmap. This member can be optional if this 
        }
        [StructLayout(LayoutKind.Sequential)]
        public struct CURSORINFO
        {
            public Int32 cbSize;        // Specifies the size, in bytes, of the structure. 
            public Int32 flags;         // Specifies the cursor state. This parameter can be one of the following values:
            public IntPtr hCursor;          // Handle to the cursor. 
            public POINT ptScreenPos;       // A POINT structure that receives the screen coordinates of the cursor. 
        }  
        [DllImport("user32.dll", EntryPoint = "GetCursorInfo")]
        public static extern bool GetCursorInfo(out CURSORINFO pci); 
        [DllImport("user32.dll", EntryPoint = "CopyIcon")]
        public static extern IntPtr CopyIcon(IntPtr hIcon); 
        [DllImport("user32.dll", EntryPoint = "GetIconInfo")]
        public static extern bool GetIconInfo(IntPtr hIcon, out ICONINFO piconinfo);
接下来,您需要这些功能:
       public static Bitmap CaptureCursor()
        {
            Bitmap bmp;
            IntPtr hicon;
            CURSORINFO ci = new CURSORINFO();
            ICONINFO icInfo;
            ci.cbSize = Marshal.SizeOf(ci);
            if (GetCursorInfo(out ci))
            {
                if (ci.flags == CURSOR_SHOWING)
                {
                    hicon = CopyIcon(ci.hCursor);
                    if (GetIconInfo(hicon, out icInfo))
                    {
                      //  x = ci.ptScreenPos.x - ((int)icInfo.xHotspot);
                      //  y = ci.ptScreenPos.y - ((int)icInfo.yHotspot);
                        Icon ic = Icon.FromHandle(hicon);
                        bmp = ic.ToBitmap(); 
                        return bmp;
                    }
                }
            }
            return null;
        }
        /*************************************************************************
         * method for comparing 2 cursor bitmaps to see if they are the same. 
         * (actually, this method works to compare any two botmaps) First
         * we convert both images to a byte array, we then get their hash (their
         * hash should match if the images are the same), we then loop through
         * each item in the hash comparing with the 2nd Bitmap
         **************************************************************************/
        public static bool CompareCursorImages(ref Bitmap bmp1, ref Bitmap bmp2)
        {
            try
            {
                //create instance or System.Drawing.ImageConverter to convert
                //each image to a byte array
                ImageConverter converter = new ImageConverter();
                //create 2 byte arrays, one for each image
                byte[] imgBytes1 = new byte[1];
                byte[] imgBytes2 = new byte[1]; 
                //convert images to byte array
                imgBytes1 = (byte[])converter.ConvertTo(bmp1, imgBytes2.GetType());
                imgBytes2 = (byte[])converter.ConvertTo(bmp2, imgBytes1.GetType()); 
                //now compute a hash for each image from the byte arrays
                SHA256Managed sha = new SHA256Managed();
                byte[] imgHash1 = sha.ComputeHash(imgBytes1);
                byte[] imgHash2 = sha.ComputeHash(imgBytes2); 
                //now let's compare the hashes
                for (int i = 0; i < imgHash1.Length && i < imgHash2.Length; i++)
                {
                    //whoops, found a non-match, exit the loop
                    //with a false value
                    if (!(imgHash1[i] == imgHash2[i]))
                        return false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }
            //we made it this far so the images must match
            return true;
        } 
    }
现在您可以调用捕获光标,并进行比较。 顺便说一句,如果您想使用它来确定某人何时在数据网格中输入了一个单元格,它会有些棘手。 为了能够进行比较,您首先需要进行比较,并且使用默认的Windows游标不是一个好主意-因为该应用程序可能已经创建了一个自定义游标。

您应该将鼠标指针移到您知道将显示默认光标的区域(即应用程序窗口的标题栏),并抓住该位图以用作基线默认图标。

FWIW。

From: https://bytes.com/topic/c-sharp/insights/887436-getting-system-cursor

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值