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

标签 c# mouse-cursor

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

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

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

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

那么,我怎样才能获得真正的光标类型状态呢? (如果是手,普通光标,调整某些东西的大小或等待等)

最佳答案

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

    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);

关于C# - 如何获取当前光标状态(如果当前是箭头、手、等待等),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25367952/

相关文章:

C# 线程 - 父访问问题

jquery - 在 Firefox 中将图像或 div 附加到鼠标光标

c# - Xamarin PCL 使用 443 以外的其他端口与 REST Api 通信

c# - Solr:如何优先搜索特殊字符

c# - 在输入字段中写入 RTL

c# - 在 XNA/C# 中添加自定义光标?

python - wxPython 更改鼠标光标以通知长时间运行的操作

.net - 如何在 UWP 应用中设置鼠标光标位置?

android avd 光标指针图像

c# - 使用 Entity Framework 的最小存储库实现