如何在c#中模拟鼠标点击?(我总是出错)

本文关键字:出错 鼠标 模拟 | 更新日期: 2023-09-27 18:05:45

好了,伙计们,事情是这样的。我使用了这个很棒的网站,并找到了一个代码片段,帮助我创建一个基本的点击脚本。问题是,我不断得到一个错误时,调试-指向我的鼠标点击行。下面,我在代码标签中添加了错误。

PInvokeStackImbalance was detected
Message: A call to PInvoke function 'MagicMouse!MagicMouse.Form1::mouse_event' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.

我已经尽了最大的努力,并且在谷歌上搜索了几次…但我什么也找不到,也许是我做错了。不管怎样,我都希望你们能帮我。下面是我的实际代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Runtime.InteropServices;
namespace MagicMouse
{
    public partial class Form1 : Form
    {
        //all this stuff has to do with being able to actually click a mouse
        [DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
        public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);
        private const int MOUSEEVENTF_LEFTDOWN = 0x02;
        private const int MOUSEEVENTF_LEFTUP = 0x04;
        private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
        private const int MOUSEEVENTF_RIGHTUP = 0x10;
        //this function will click the mouse using the parameters assigned to it
        public void DoMouseClickLeft()
        {
            //Call the imported function with the cursor's current position
            int X = Cursor.Position.X;
            int Y = Cursor.Position.Y;
            mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
        }
        public void NumberCheck(string ValidateNum)
        {
            long CanConvertOut = 0;
            bool CanConvertBool = long.TryParse(ValidateNum, out CanConvertOut);
            if (CanConvertBool == false)
            {
                MessageBox.Show("Please enter a valid number");
                return;
            }
        }
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }
        //button 1 is start alching
        private void button1_Click(object sender, EventArgs e)
        {
            //this section sends data to the number validation function
            string ValidateNum = NumberOfItems.Text;
            NumberCheck(ValidateNum);
            int Alchs = Convert.ToInt16(NumberOfItems.Text);
            for (int i = 1; i < Alchs; i++)
            {
                DoMouseClickLeft();
            }
        }
        //button 2 is exit
        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

    }
}

如何在c#中模拟鼠标点击?(我总是出错)

这正是错误消息所说的-将DllImport声明更改为:

[DllImport("user32.dll")]
static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint dwData,
   UIntPtr dwExtraInfo);

更多信息请参见http://pinvoke.net/default.aspx/user32.mouse_event

但由于mouse_event已弃用,请考虑使用SendInput

:

  • http://msdn.microsoft.com/en-us/library/ms646310.aspx
  • http://www.pinvoke.net/default.aspx/user32/SendInput.html