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

C# 使用C在另一个应用程序上执行鼠标单击事件,c#,mouseevent,simulate,C#,Mouseevent,Simulate,我需要做的是,我需要使用我的自定义应用程序控制安装在同一台机器上的另一个应用程序。例如,如果我需要使用标准的windows计算器,我只需将输入事件发送到计算器。我使用了一些代码片段来实现这一点,现在我触发了鼠标和键盘事件。但问题是,我可以确定键盘事件将命中目标应用程序,因为它具有进程句柄。但是我不能确定老鼠的情况。而且,如果目标应用程序进入后台,我无法启动鼠标单击。我需要帮助,以找到一种方法,以确保鼠标点击是在应用程序上只做 我需要发送鼠标坐标并单击。例如sendMouseClickNotepa

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

我需要发送鼠标坐标并单击。例如sendMouseClickNotepad,100400;这将发送一个点击记事本,即使它保持最小化

*****************************************************重要提示***************************************************** 前面回答了一个类似的问题,但这是关于首先查找另一个应用程序的状态,然后通过键盘或鼠标发送输入,我需要做的是向应用程序发送一组指令,这些指令必须工作,无论应用程序是否在前台。 对于其他人:如果你不想帮忙或帮不上忙,那没关系,但请务必知道我没有偷这个问题或任何东西。我只是想用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和其他站点的帮助提供了在同一应用程序中模拟鼠标单击的代码。但是我需要将鼠标点击发送到另一个应用程序。

也许这可以帮助您

密码 任务

获取鼠标的当前位置 发送鼠标事件 Windows窗体 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); 
    ...
警告 代码已经过测试 该代码不是复制粘贴代码
也许这对你有帮助

密码 任务

获取鼠标的当前位置 发送鼠标事件 Windows窗体 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); 
    ...
警告 代码已经过测试 该代码不是复制粘贴代码
可能重复的P/Invoke实际上没有。我不需要创建一个机器人。我正在尝试为另一个软件创建一种驱动程序。但是我不能只使用按键,我还需要启动moust事件。我正在尝试用C语言而不是其他语言来做这件事…@SriramSakthivel先生,我知道可以使用win32 api来移动鼠标,但这不起作用,因为如果用户从应用程序切换焦点,鼠标事件可能会击中其他应用程序。SendKeys.SendWait发送键,我需要类似的鼠标。如果您知道窗口句柄,请使用PostMessage。你可以得到一个P/Invoke的可能副本,实际上不。我不需要创建一个bot。我正在尝试为另一个软件创建一种驱动程序。但是我不能只使用按键,我还需要启动moust事件。我正在尝试用C语言而不是其他语言来做这件事…@SriramSakthivel先生,我知道可以使用win32 api来移动鼠标,但这不起作用,因为如果用户从应用程序切换焦点,鼠标事件可能会击中其他应用程序。SendKeys.SendWait发送键,我需要类似的鼠标。如果您知道窗口句柄,请使用PostMessage。你可以得到一份工作