Имитация кликов мышки в нескольких приложениях - C#
Формулировка задачи:
Всем привет!!!
Есть несколько окон(от 2х до 4х) в которых запущено одно и тоже приложение (игра, ворд, огнелис не важно). Нужно одновременно выполнять в них имитацию кликов мышки по координатам этих приложений!
После гугления нашел несколько статей, но во всех показывается как работать только с одним свёрнутым окном, а как это будет выглядеть для нескольких окон, и будет ли свободны движения мыши на основном экране!
Решение задачи: «Имитация кликов мышки в нескольких приложениях»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//using System;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
private Button button1 = new Button();
public Form1()
{
button1.Location = new Point(10, 10);
button1.TabIndex = 0;
button1.Text = "Click to automate Calculator";
button1.AutoSize = true;
button1.Click += new EventHandler(button1_Click);
this.DoubleClick += new EventHandler(Form1_DoubleClick);
this.Controls.Add(button1);
}
[Flags]
public enum WindowMessages : uint
{
WM_MOUSEMOVE = 0x200,
WM_LBUTTONDOWN = 0x201,
WM_RBUTTONDOWN = 0x204,
WM_MBUTTONDOWN = 0x207,
WM_LBUTTONUP = 0x202,
WM_RBUTTONUP = 0x205,
WM_MBUTTONUP = 0x208,
WM_LBUTTONDBLCLK = 0x203,
WM_RBUTTONDBLCLK = 0x206,
WM_MBUTTONDBLCLK = 0x209,
WM_MOUSEWHEEL = 0x020A,
WM_KEYDOWN = 0x100,
WM_KEYUP = 0x101,
WM_SYSKEYDOWN = 0x104,
WM_SYSKEYUP = 0x105,
}
// 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);
[DllImport("user32.dll")]
public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
// 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("SciCalc","Калькулятор");
// 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("111");
//SendKeys.SendWait("*");
//SendKeys.SendWait("11");
//SendKeys.SendWait("=");
SendMessage(calculatorHandle, (uint) WindowMessages.WM_LBUTTONDOWN, 685, 264);
SendMessage(calculatorHandle, (uint) WindowMessages.WM_LBUTTONUP, 685, 264);
}
// 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}");
}
}
}