トップ回答者
マウスをプログラムからクリックさせたい

質問
-
はじめまして。
今プログラムからマウスを移動させクリックさせたいと思っていますがうまくクリックできません。
ボタン1をクリックするとボタン2がクリックされメッセージボックスを表示させるはずのプログラムです。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;namespace WindowsApplication1
{
public partial class Form1 : Form
{
[DllImport("user32.dll")]
extern static uint SendInput(
uint nInputs,
INPUT[] pInputs,
int cbSize);
[StructLayout(LayoutKind.Sequential)]
struct INPUT
{
public int type;
public MOUSEINPUT mi;
}[StructLayout(LayoutKind.Sequential)]
struct MOUSEINPUT
{
public int dx;
public int dy;
public int dwFlags;
public int mouseData;
public IntPtr dwExtraInfo;
}
const int MOUSEEVENTF_MOVED = 0x0001;
const int MOUSEEVENTF_LEFTDOWN = 0x0002;
const int MOUSEEVENTF_LEFTUP = 0x0004 ;
const int MOUSEEVENTF_ABSOLUTE = 0x8000;
const int screen_length = 0x10000;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
INPUT[] input = new INPUT[3];
Cursor.Position = new Point(300, 300);
input[0].mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
input[1].mi.dx = screen_length / 2;
input[1].mi.dy = screen_length / 2;
input[1].mi.dwFlags = MOUSEEVENTF_MOVED | MOUSEEVENTF_ABSOLUTE;
input[2].mi.dwFlags = MOUSEEVENTF_LEFTUP;
SendInput(3, input, Marshal.SizeOf(input[0]));
System.Threading.Thread.Sleep(1000);
}private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show("hello world");
}}
どこに問題があるのでしょうか?
ぜひご教授お願いします。