C# - 如何获取当前光标状态(如果当前是箭头、手、等待等)

use*_*993 2 c# mouse-cursor

我试图为一款游戏制作一个机器人,但他们有很酷的反像素机器人技术。

所以我想,“如果我可以制作一个机器人,只检查光标是否变为手形,然后单击,它就会起作用,”因为我需要收集奖励框,当你将光标指向它时,它会变为“手”光标。

所以我对这个想法非常高兴,但是在 C# 中,它不起作用!

在 C# 中 -Cursor.Current只检查表单上的光标状态,而不是整个计算机中的光标状态,这不是我想要的。

那么,如何获得真正的光标类型状态呢?(如果是手,正常光标,调整某些东西的大小或等待等)

use*_*993 5

好的,我找到了一些东西并使其工作,如果有人需要的话,这里是代码:

    private static string GetCursorState()
    {
        var h = Cursors.WaitCursor.Handle;

        CURSORINFO pci;
        pci.cbSize = Marshal.SizeOf(typeof(CURSORINFO));
        GetCursorInfo(out pci);


        return pci.hCursor.ToString();
    }

    [StructLayout(LayoutKind.Sequential)]
    struct POINT
    {
        public Int32 x;
        public Int32 y;
    }

    [StructLayout(LayoutKind.Sequential)]
    struct CURSORINFO
    {
        public Int32 cbSize;        // Specifies the size, in bytes, of the structure. 
        // The caller must set this to Marshal.SizeOf(typeof(CURSORINFO)).
        public Int32 flags;         // Specifies the cursor state. This parameter can be one of the following values:
        //    0             The cursor is hidden.
        //    CURSOR_SHOWING    The cursor is showing.
        public IntPtr hCursor;          // Handle to the cursor. 
        public POINT ptScreenPos;       // A POINT structure that receives the screen coordinates of the cursor. 
    }

    [DllImport("user32.dll")]
    static extern bool GetCursorInfo(out CURSORINFO pci);
Run Code Online (Sandbox Code Playgroud)