title: Perform a mouse Click event on another Application using C#
body: <p>what i need to do is that I need to control another application installed on the same machine using my custom application. For example if i needed to use the standard windows calculator I would simply send the input events to the calculator. I have used some code snippets to make this possible and I have now triggered both mouse and keyboard events. but the problem is that i can be sure that the keyboard event will hit the target application because it has the process handle. but i cannot be sure about the mouse. and also if the target application goes into background, i cannot initiate mouse clicks on it. I need help to find a way to make sure that the mouse click is done on the application only.</p>
  <p>I need to send mouse co-ordinates and click as well.  for example &quot;<code>sendMouseClick(&quot;Notepad&quot;, 100, 400);</code> which will send a click to Notepad, even though it stays minimized.</p>
  <p><strong>IMPORTANT NOTE</strong></p>
  <p>A similar question is answered previously but that is in reference to first finding the state of the other application and then sending the inputs either keyboard or mouse, what i need to do is to send an application a set of instructions that must work whether the application is in foreground or not.
  For &quot;The other Guys&quot;:: if you dont want to help or cant help, thats okay but please do know that i havent stolen the question or anything. I simply want to achieve this task in C#.</p>
  <p>The code I have to simulate keyboard key Press is:</p>
  <pre><code>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 = &quot;Click to automate Calculator&quot;;
          button1.AutoSize = true;
          button1.Click += new EventHandler(button1_Click);
  
          button2.Location = new Point(150, 140);
          button2.TabIndex = 0;
          button2.Text = &quot;Click to Exit Calculator&quot;;
          button2.AutoSize = true;
  
          button2.Location = new Point(80, 80);
          button2.TabIndex = 2;
          button2.Text = &quot;Click to Run Calculator&quot;;
          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(&quot;USER32.DLL&quot;, CharSet = CharSet.Unicode)]
      public static extern IntPtr FindWindow(string lpClassName,
          string lpWindowName);
  
      // Activate an application window.
      [DllImport(&quot;USER32.DLL&quot;)]
      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(&quot;CalcFrame&quot;, &quot;Calculator&quot;);
          
          
          //Process firstProc = new Process();
          //firstProc.StartInfo.FileName = &quot;calc.exe&quot;;
          //firstProc.EnableRaisingEvents = true;
  
          //firstProc.Start();
          
          // Verify that Calculator is a running process. 
          if (calculatorHandle == IntPtr.Zero)
          {
              MessageBox.Show(&quot;Calculator is not running.&quot;);   
              return;
          }
  
          // Make Calculator the foreground application and send it  
          // a set of calculations.
          SetForegroundWindow(calculatorHandle);
          SendKeys.SendWait(&quot;1024&quot;);
          SendKeys.SendWait(&quot;*&quot;);
          SendKeys.SendWait(&quot;32&quot;);
          SendKeys.SendWait(&quot;=&quot;);
      }
      private void button2_Click(object sender, EventArgs e)
      {
          System.Diagnostics.Process.Start(&quot;calc.exe&quot;);
          
      }
      private void button3_Click(object sender,EventArgs e) 
      {
          Process [] proc =Process.GetProcessesByName(&quot;Calculator&quot;);
          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(&quot;{ENTER}&quot;);
      }
  }
  }
  </code></pre>
  <p>the previous help on stack overflow, msdn and other sites provides the code to simulate a mouse click in the same application. But i need to send mouse hits to another application.</p>