Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 光标.位置和鼠标\u事件被阻止_C#_Click_Cursor_Mouse - Fatal编程技术网

C# 光标.位置和鼠标\u事件被阻止

C# 光标.位置和鼠标\u事件被阻止,c#,click,cursor,mouse,C#,Click,Cursor,Mouse,我正在开发一个应用程序,以自动将一些输入到另一个应用程序中。我遇到了一个问题。下面是我正在使用的函数代码 public class MouseClick { [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] public static extern void mouse_event(int dwFlags, int dx, int dy,

我正在开发一个应用程序,以自动将一些输入到另一个应用程序中。我遇到了一个问题。下面是我正在使用的函数代码

public class MouseClick
{
    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
    public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
}

public enum MouseButton
{
    MOUSEEVENTF_LEFTDOWN = 0x02,
    MOUSEEVENTF_LEFTUP = 0x04,
    MOUSEEVENTF_RIGHTDOWN = 0x08,
    MOUSEEVENTF_RIGHTUP = 0x10
}
这是我用来移动和点击的代码

Point LocPoint = GetLocation(Column, Row, Item);
Console.WriteLine("Column: {0}\tRow: {1}\tItem: {2}\tPoints: {3}\tCursor: {4}", Column, Row, Item, Points, LocPoint.X + "," + LocPoint.Y);

Thread.Sleep(200);
Cursor.Position = LocPoint;
Thread.Sleep(10);

MouseClick.mouse_event((int)MouseButton.MOUSEEVENTF_LEFTDOWN | (int)MouseButton.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
Thread.Sleep(200);
然而,当我没有要输入的应用程序作为活动窗口,但说mspaint代码运行良好,我从画笔中得到点,我想点击的地方,事情就变得有趣了。然而,当我要运行的应用程序处于活动状态时,鼠标永远不会移动,没有点击,就好像应用程序正在拦截这些调用并忽略它们。这就引出了两个问题

  • 这可能吗?它如何检测鼠标和设置跳线之间的差异
  • 有没有一种方法可以绕过这一点或使用另一种方法
  • 好的,所以每个人都告诉我使用SendInput。好的,我将代码改为使用SendInput。我还尝试了C#SendKeys作为测试。目前,我已经回到了基础,我只是尝试将字母A输入到一个文本输入框中,我手动创建了目标。当我在记事本中运行它时,SendInput和SendKeys都会键入字母A,但是当我在另一个应用程序中时,我会尝试自动执行此操作,直到什么都没有显示。这是我正在使用的SendInput代码

    INPUT[] Inputs = new INPUT[2];
    Inputs[0].type = WindowsAPI.INPUT_KEYBOARD;
    Inputs[0].ki.wVk = 0;
    Inputs[0].ki.dwFlags = WindowsAPI.KEYEVENTF_UNICODE;
    Inputs[0].ki.wScan = 0x41;
    
    Inputs[0].type = WindowsAPI.INPUT_KEYBOARD;
    Inputs[0].ki.wVk = 0;
    Inputs[1].ki.dwFlags = WindowsAPI.KEYEVENTF_KEYUP;
    Inputs[0].ki.wScan = 0x41;
    
    WindowsAPI.SendInput((uint)Inputs.Length, Inputs, Marshal.SizeOf(Inputs[0]));
    

    因此,在经历了挫折之后,我决定在VisualStudio的调试模式之外运行该应用程序,这导致了相同的结果。然而,我决定以“管理员”的身份运行应用程序,即使我已经关闭了UAC的所有功能,令我惊讶的是,应用程序正确地移动了鼠标,点击并输入了文本。我不知道为什么需要这样做,因为我以前使用过Pinvok方法,也从来没有这样做过,但这似乎是解决方案。

    您忘记了MOUSEEVENTF_MOVE=0x01;鼠标事件()很久以前就被弃用了。它有一个严重的问题,它不能告诉你你用错了。它没有BOOL返回值。因此,除了“它不工作”之外,您永远无法发现代码中存在bug。改为使用SendInput()。从pinvoke.net这样的网站获取正确的声明,您的鼠标事件()声明是错误的。使用附加测试更新了问题