【问题标题】:Move Mouse to Position and Left Click将鼠标移动到位置并左键单击
【发布时间】:2012-11-11 07:46:43
【问题描述】:

我正在使用 C#、Framework 4(32 位)编写一个 Windows 窗体应用程序。

我有一个包含鼠标坐标的列表,我可以捕获它们。到目前为止一切顺利。

但在某些时候,我想转到那些坐标并单击鼠标左键。

这就是现在的样子:

for (int i = 0; i < coordsX.Count; i++)
{
    Cursor.Position = new Point(coordsX[i], coordsY[i]);
    Application.DoEvents();
    Clicking.SendClick();
}

还有 Clicking 类:

class Clicking
    {
        private const UInt32 MOUSEEVENTF_LEFTDOWN = 0x0002;
        private const UInt32 MOUSEEVENTF_LEFTUP = 0x0004;
        private static extern void mouse_event(
               UInt32 dwFlags, // motion and click options
               UInt32 dx, // horizontal position or change
               UInt32 dy, // vertical position or change
               UInt32 dwData, // wheel movement
               IntPtr dwExtraInfo // application-defined information
        );

        // public static void SendClick(Point location)
        public static void SendClick()
        {
            // Cursor.Position = location;
            mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, new System.IntPtr());
            mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, new System.IntPtr());
        }
    }

但我收到此错误:

Could not load type 'program.Clicking' from assembly 'program, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' because the method 'mouse_event' has no implementation (no RVA).

我真的不明白问题是什么......你们知道问题是什么吗?或者你知道更好的方法来做我想做的事吗?

【问题讨论】:

    标签: c# mousemove mouseclick-event sendinput


    【解决方案1】:

    您是否包含以下行?

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

    这将从user32 dll 中导入函数mouse_event,这是您尝试在程序中使用的函数。目前,您的程序不知道 DLL 中的此方法,除非您指定它来自何处。

    PInvoke.net user32 Mouse Event 网站对于这类事情的基础知识非常方便。

    Directing mouse events [DllImport(“user32.dll”)] click, double click的回答也会对你的理解有很大帮助。

    flags 是您要发送到mouse_input 函数的命令,在该示例中,您可以看到他在同一行中同时发送mouse downmouse up,这很好,因为@ 987654331@ 函数会将这些标志拆分并连续执行。


    另请注意,此方法已被SendInput 命令取代,SendInputSetMousePos 的一个很好的例子可以找到At this Blog

    【讨论】:

    • 我将该行插入到点击类中,但仍然是同样的错误
    • 您是否删除了您对mouse_event 的声明?确保你这样做并用这个替换它。
    • 那 sendinput 如何特别针对我的问题工作?因为我对此很陌生,我不知道是否需要链接中的所有代码...
    • SendInput 是一个奇怪的野兽,你能做的最好的事情就是实验,我已经有一段时间没有这样做了,我不知道你想解决什么问题,但我的建议是包含所有代码并删除内容,直到它不起作用。这就是我通常做的:P,也就是说,我认为没有人会责怪你继续使用mouse_input,因为如果你只是一个初学者,它会简单得多。
    • dwFlags 是您发送的命令类型,人们将其定义为各种各样的东西,但我觉得uint 是最准确的,因为它是unsigned integer,没有负面标志.我用更多信息编辑了我的答案。
    【解决方案2】:

    我猜你错过了以下行

    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
    

    【讨论】:

    • 引发错误:属性“DllImport”在此声明类型上无效。它仅对“方法”声明有效。
    猜你喜欢
    • 1970-01-01
    • 2018-10-07
    • 2019-03-12
    • 1970-01-01
    • 1970-01-01
    • 2017-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多