代码之家  ›  专栏  ›  技术社区  ›  Sherif

如何将鼠标光标位置设置为C中屏幕上指定的点?

  •  4
  • Sherif  · 技术社区  · 15 年前

    如何在C_中将鼠标光标位置设置为屏幕上的指定点?

    我必须破解接收鼠标和键盘坐标和按键的主板缓冲区吗????

    是否还有另一个点击或者我可以想象?你说什么?

    1 回复  |  直到 15 年前
        1
  •  7
  •   Luke Quinane George    15 年前

    下面将设置鼠标位置并执行单击操作:

    public static void ClickSomePoint() {
        // Set the cursor position
        System.Windows.Forms.Cursor.Position = new Point(20, 35);
    
        DoClickMouse(0x2); // Left mouse button down
        DoClickMouse(0x4); // Left mouse button up
    }   
    
    static void DoClickMouse(int mouseButton) {
        var input = new INPUT() {
            dwType = 0, // Mouse input
            mi = new MOUSEINPUT() { dwFlags = mouseButton }
        };
    
        if (SendInput(1, input, Marshal.SizeOf(input)) == 0) { 
            throw new Exception();
        }
    }
    [StructLayout(LayoutKind.Sequential)]
    struct MOUSEINPUT {
        int dx;
        int dy;
        int mouseData;
        public int dwFlags;
        int time;
        IntPtr dwExtraInfo;
    }   
    struct INPUT {
        public uint dwType;
        public MOUSEINPUT mi;
    }   
    [DllImport("user32.dll", SetLastError=true)]
    static extern uint SendInput(uint cInputs, INPUT input, int size);
    

    记住这可能是 极其 对用户来说很烦人。

    :)


    如果要单击表单上的按钮,可以使用 'PerformClick()' 方法。