C#で任意の座標にマウスポインタを移動し、クリックイベントを実行するWPFサンプルです。
MainWindow.xaml
<Window x:Class="MouseCommander.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="90" Width="250">
<Window.Resources>
<ResourceDictionary>
<Style TargetType="Button">
<Setter Property="Margin" Value="10 0 0 0"></Setter>
<Setter Property="Width" Value="100"></Setter>
<Setter Property="Height" Value="30"></Setter>
</Style>
</ResourceDictionary>
</Window.Resources>
<Grid>
<StackPanel Orientation="Horizontal"
Margin="10"
Height="30" HorizontalAlignment="Right" VerticalAlignment="Bottom">
<Button Name="startButton" Click="StartButton_Click" >開始</Button>
<Button Name="stopButton" Click="StopButton_Click">終了</Button>
</StackPanel>
</Grid>
</Window>
MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace MouseCommander
{
/// <summary>
/// MainWindow.xaml の相互作用ロジック
/// </summary>
public partial class MainWindow : Window
{
[DllImport("USER32.dll", CallingConvention = CallingConvention.StdCall)]
private static extern void SetCursorPos(int X, int Y);
[DllImport("USER32.dll", CallingConvention = CallingConvention.StdCall)]
private static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
private const int MOUSEEVENTF_LEFTDOWN = 0x2;
private const int MOUSEEVENTF_LEFTUP = 0x4;
public MainWindow()
{
InitializeComponent();
}
private void StartButton_Click(object sender, RoutedEventArgs e)
{
var stopButtonPosition = stopButton.PointToScreen(new Point(0, 0));
SetCursorPos((int)stopButtonPosition.X + 1, (int)stopButtonPosition.Y + 1);
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
}
private void StopButton_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("終了");
}
}
}