c# - 如何在C#中模拟鼠标点击? (我一直收到错误)

标签 c#

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

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

        }


    }
}

最佳答案

这正是错误消息所说的 - 将 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

参见:

关于c# - 如何在C#中模拟鼠标点击? (我一直收到错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7055252/

相关文章:

c# - Linq 查询返回最便宜的产品,前提是它是唯一的

c# - 依赖于 Visual C++ 2013 运行时的 NuGet 包

c# - WCF 数据服务限制返回字段的能力

c# - 为什么 chromedriver 在所有运行时都重新下载 adblock 扩展?

c# - 替换 "sealed"类的实现

c# - 读取嵌入为资源的 .txt

c# - 是否可以在使用 SalesForce Soap API 插入之前向自定义对象添加附件?

c# - 使用 DirectSound 向后读取声音

c# - asp.net 如何在 asp 中使用页内链接关闭对话框

c# - 如何在 C# 中为 protobuf map<string, string> 属性设置值