【问题标题】:C# - How to get current cursor state (if its currently arrow,hand,waiting etc)C# - 如何获取当前光标状态(如果它当前是箭头、手、等待等)
【发布时间】:2021-12-31 02:10:47
【问题描述】:

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

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

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

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

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

【问题讨论】:

标签: c# mouse-cursor


【解决方案1】:

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

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

【讨论】:

  • 您可以使用 idk.. timer 获取光标 ID,例如 "this.Text = GetCursorState().ToString()" .. enjoy
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-13
  • 2023-03-17
相关资源
最近更新 更多