使用 C# 在另一个应用程序上执行鼠标单击事件

2023-12-10

我需要做的是,我需要使用我的自定义应用程序来控制安装在同一台计算机上的另一个应用程序。例如,如果我需要使用标准 Windows 计算器,我只需将输入事件发送到计算器。我使用了一些代码片段来实现这一点,现在我已经触发了鼠标和键盘事件。但问题是我可以确定键盘事件会击中目标应用程序,因为它具有进程句柄。但我不能确定鼠标。而且,如果目标应用程序进入后台,我无法对其进行鼠标单击。我需要帮助找到一种方法来确保仅在应用程序上单击鼠标。

我需要发送鼠标坐标并单击。例如 ”sendMouseClick("Notepad", 100, 400);这将向记事本发送点击,即使它保持最小化。

重要的提示

之前回答过类似的问题,但这是指首先查找另一个应用程序的状态,然后发送键盘或鼠标的输入,我需要做的是向应用程序发送一组必须工作的指令,无论该应用程序是否在前台。 对于“其他人”::如果您不想提供帮助或无法提供帮助,那没关系,但请注意我没有窃取问题或任何内容。我只是想用 C# 来完成这个任务。

我必须模拟键盘按键的代码是:

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Windows.Forms;

namespace SimulateKeyPress
{
    partial class Form1 : Form
{
    private Button button1 = new Button();
    private Button button2 = new Button();
    private Button button3 = new Button();

    [STAThread]
    public static void Main()
    {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
    }

    public Form1()
    {
        button1.Location = new Point(10, 10);
        button1.TabIndex = 1;
        button1.Text = "Click to automate Calculator";
        button1.AutoSize = true;
        button1.Click += new EventHandler(button1_Click);

        button2.Location = new Point(150, 140);
        button2.TabIndex = 0;
        button2.Text = "Click to Exit Calculator";
        button2.AutoSize = true;

        button2.Location = new Point(80, 80);
        button2.TabIndex = 2;
        button2.Text = "Click to Run Calculator";
        button2.AutoSize = true;

        button2.Click += new EventHandler(button2_Click);
        this.DoubleClick += new EventHandler(Form1_DoubleClick);

        this.Controls.Add(button1);
        this.Controls.Add(button2);
       // this.Controls.Add(button3);
    }

    // Get a handle to an application window.
    [DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
    public static extern IntPtr FindWindow(string lpClassName,
        string lpWindowName);

    // Activate an application window.
    [DllImport("USER32.DLL")]
    public static extern bool SetForegroundWindow(IntPtr hWnd);

    // Send a series of key presses to the Calculator application. 
    private void button1_Click(object sender, EventArgs e)
    {
        // Get a handle to the Calculator application. The window class 
        // and window name were obtained using the Spy++ tool.
        IntPtr calculatorHandle = FindWindow("CalcFrame", "Calculator");
        
        
        //Process firstProc = new Process();
        //firstProc.StartInfo.FileName = "calc.exe";
        //firstProc.EnableRaisingEvents = true;

        //firstProc.Start();
        
        // Verify that Calculator is a running process. 
        if (calculatorHandle == IntPtr.Zero)
        {
            MessageBox.Show("Calculator is not running.");   
            return;
        }

        // Make Calculator the foreground application and send it  
        // a set of calculations.
        SetForegroundWindow(calculatorHandle);
        SendKeys.SendWait("1024");
        SendKeys.SendWait("*");
        SendKeys.SendWait("32");
        SendKeys.SendWait("=");
    }
    private void button2_Click(object sender, EventArgs e)
    {
        System.Diagnostics.Process.Start("calc.exe");
        
    }
    private void button3_Click(object sender,EventArgs e) 
    {
        Process [] proc =Process.GetProcessesByName("Calculator");
        proc[0].Kill();
    }
    // Send a key to the button when the user double-clicks anywhere  
    // on the form. 
    private void Form1_DoubleClick(object sender, EventArgs e)
    {
        // Send the enter key to the button, which raises the click  
        // event for the button. This works because the tab stop of  
        // the button is 0.
        SendKeys.Send("{ENTER}");
    }
}
}

之前关于堆栈溢出、msdn 和其他站点的帮助提供了在同一应用程序中模拟鼠标单击的代码。但我需要将鼠标点击发送到另一个应用程序。


也许这可以帮助你

Code

The task

  • 获取鼠标当前位置
  • 发送鼠标事件

Windows 窗体

...
using System.Runtime.InteropServices;

namespace yournamespace
{

 public partial class yourclassname
 {
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
    public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint 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;

    int X = Cursor.Position.X;
    int Y = Cursor.Position.Y;
    mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
  }
}   

WPF

WPF 中的事情有点困难

double mousePointX;
double mousePointY;
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetCursorPos(out POINT lpPoint);

[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
  public int X;
  public int Y;

  public POINT(int x, int y)
  {
      this.X = x;
      this.Y = y;
  }
}

private void WritePoint(object sender, RoutedEventArgs e)
{
    POINT p;
    if (GetCursorPos(out p))
    {
        System.Console.WriteLine(Convert.ToString(p.X) + ";" + Convert.ToString(p.Y));
    }
}
[DllImport("User32.dll")]
static extern IntPtr GetDC(IntPtr hwnd);

[DllImport("gdi32.dll")]
static extern int GetDeviceCaps(IntPtr hdc, int nIndex);

[DllImport("user32.dll")]
static extern bool ReleaseDC(IntPtr hWnd, IntPtr hDC);

private Point ConvertPixelsToUnits(int x, int y)
{
    // get the system DPI
    IntPtr dDC = GetDC(IntPtr.Zero); // Get desktop DC
    int dpi = GetDeviceCaps(dDC, 88);
    bool rv = ReleaseDC(IntPtr.Zero, dDC);

    // WPF's physical unit size is calculated by taking the 
    // "Device-Independant Unit Size" (always 1/96)
    // and scaling it by the system DPI
    double physicalUnitSize = (1d / 96d) * (double)dpi;
    Point wpfUnits = new Point(physicalUnitSize * (double)x,
    physicalUnitSize * (double)y);

    return wpfUnits;          
}
private void WriteMouseCoordinatesInWPFUnits()
{
    POINT p;
    if (GetCursorPos(out p))
    {
    Point wpfPoint = ConvertPixelsToUnits(p.X, p.Y);
    System.Console.WriteLine(Convert.ToString(wpfPoint.X) + ";" +   Convert.ToString(wpfPoint.Y));

    mousePointY = wpfPoint.Y;
    mousePointX = wpfPoint.X
    }
}

现在是代码中最重要的部分

 [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
    public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint 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;

    ...    
        mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP,   Convert.ToUInt32(mousePointX), Convert.ToUInt32(mousePointY), 0, 0); 
    ...

Warning

  • 代码已测试
  • 该代码不是“复制粘贴代码”
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 C# 在另一个应用程序上执行鼠标单击事件 的相关文章

随机推荐

  • Reporting Services 2008 - 长子报表导致分页

    类似这个问题 但我有一个 主 报告 其中包含许多子报告 一切都运行良好 直到其中一个子报表在渲染时 例如 PDF 将占据页面上剩余的空间 在这种情况下 分页会导致子报表在新页面上开始 有时 当一个报表仅占用一页上的一点空间 然后由于以下子报
  • 在R中创建二元指示矩阵(布尔矩阵)

    我有一个表明参加会议的数据列表 如下所示 Event Participant ConferenceA John ConferenceA Joe ConferenceA Mary ConferenceB John ConferenceB Te
  • Symfony2:学说:PHPUnit:在单元测试中使用模拟实体管理器刷新期间设置实体 ID

    Symfony 2 8 13 Doctrine ORM 2 5 5 PHPUnit 5 7 5 我想测试一个使用学说实体管理器的类的方法 此公共方法调用一个私有方法来实例化 Bookmark 实体 刷新它并返回该实体 然后 在测试的方法中我
  • 不使用 for-each 旋转 背景颜色

    我正在尝试设置其他每个的背景颜色 tr 我没有使用 for each 循环 因为我使用一些模板来根据节点值过滤数据 tr
  • ASP.NET C# 使用 System.IO 复制目录和子目录

    我需要将整个目录 C X 复制到 C Y X 并且还需要复制子文件夹 有什么方法可以使用 System IO File Directory 命名空间来做到这一点吗 感谢所有帮助者 此类将复制或移动文件夹 无需递归调用 这些方法使用自己的堆栈
  • UTF-8 字符串分隔符

    我正在解析一个二进制协议 其中 UTF 8 字符串散布在原始字节中 此特定协议在每个 UTF 8 字符串前面添加一个短字符 两个字节 指示后续 UTF 8 字符串的长度 这给出了最大字符串长度 2 16 gt 65 000 这对于特定应用来
  • 如何触发特定的Google自定义搜索引擎细化标签?

    目前 我们的组织正在使用 Google 自定义搜索引擎来提供自动建议 并且我们在 CSE 中配置了大约 3 个优化标签 之前 我们使用 WebSearch 和 SearchControl WebSearch 有一个 setSiteRestr
  • QWizard:更改标题字段的高度/尺寸

    我目前正在尝试为我正在开发的 Python Qt 应用程序实现一个简单的 第一步 向导 使用 Designer 确实很容易做到这一点 但与往常一样 问题在于细节 我的问题是 标题 字段对我来说太大了 约占可用屏幕空间的 50 这里有一个截屏
  • 给定表数据的期望输出

    您好 我有一个表测试其结构如下 Testing PK C1 c2 1 v11 v12 2 v21 v23 3 v31 v32 现在我需要查询该表 测试 以便获得以下输出 Pk Key value 1 c1 v11 1 c1 v12 2 c2
  • 泛型类型约束与继承

    这两个函数声明有区别吗 func doSomething
  • 在 C# 中将很长的日期格式解析为 DateTime

    如何将以下字符串日期解析为 C 中的 DateTime 对象 1970 年 1 月 1 日 星期四 这是来自 XML 提要 而 DateTime Parse 似乎不喜欢 en GB 语言环境中的它 该提要仅来自英国服务器 因此我不必担心全球
  • YUV NV21 转换为 RGB 的困惑

    根据http developer android com reference android graphics ImageFormat html NV21 NV21 是默认使用的格式 网上有很多关于 YUV NV21 到 RGB 转换的代码
  • OpenGL 中纹理像素和屏幕像素之间的一对一映射

    我正在使用 OpenGL 执行以下操作 我的屏幕尺寸是512 512 我有一个与屏幕大小相同的纹理 我想绘制一个覆盖整个屏幕的四边形 并在像素和纹素之间建立一对一的映射 这样我就可以做到texelFetch与着色器中的屏幕坐标 首先 如果我
  • 何时在新类名后添加括号? [复制]

    这个问题在这里已经有答案了 可能的重复 PHP 类实例化 使用或不使用括号 省略括号和无参数对象构造函数 不管有没有括号 新的Class似乎都不麻烦 所以我怀疑括号有什么用 我查了php手册 没有明白 有人能解释一下吗 括号的目的是让您输入
  • 在哪些情况下会跳过 DAG 的阶段?

    我正在尝试查找在我使用 RDD 的情况下 Spark 会跳过阶段的情况 我知道如果发生洗牌操作 它会跳过阶段 于是 我写了下面的代码来看看是否正确 def main args Array String Unit val conf new S
  • 如何阻止代码/程序访问Web应用程序?

    我想阻止任何人尝试通过代码访问我的网站 从代码的意义上来说 任何一个类似于bot的程序 都会造成流量激增 例如 URL url new URL http www example com URLConnection yc url openCo
  • 使用 jquery 调用 ajax 后如何等待图像加载?

    我有一个 Python 脚本 它对 JPEG 图像进行一些操作 我将一些参数传递给此脚本并从 HTML 页面调用它 该脚本返回一个 img src newimage jpg 标签 我知道如何等待脚本的回复 但我不知道如何判断图像何时完全加载
  • 由于 RAM 故障,是否会发生 System.AccessViolationException?

    我有一个网站 我已经工作了大约一年了 最近 它开始时不时地在我的开发机器中抛出 System AccessViolationException 大多数情况下 当我离开机器时 几周以来我没有对服务器代码进行任何更改 因为我一直在 100 地处
  • cmake 错误:QGLWidget:没有这样的文件或目录

    我正在尝试将 cmake 与 Qt 一起使用 但是在构建时出现以下错误 QGLWidget No such file or directory 在我的 CMakeLists txt 文件中 我有 set CMAKE INCLUDE CURR
  • 使用 C# 在另一个应用程序上执行鼠标单击事件

    我需要做的是 我需要使用我的自定义应用程序来控制安装在同一台计算机上的另一个应用程序 例如 如果我需要使用标准 Windows 计算器 我只需将输入事件发送到计算器 我使用了一些代码片段来实现这一点 现在我已经触发了鼠标和键盘事件 但问题是