特定のキーを押し続けた場合に送信されるウィンドウに同じメッセージを送信するプログラムを実装しようとしています。これは、アプリケーションのコードの一部です(Form1.csコード全体はここにあります)。
[DllImport("User32.dll")]
static extern int SendMessage(IntPtr hWnd, uint wMsg, UIntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, IntPtr windowTitle);
private void button1_Click(object sender, EventArgs e)
{
// find notepad process
System.Diagnostics.Process[] p = System.Diagnostics.Process.GetProcessesByName("notepad");
// find child handle of notepad process
// that is where we can send WM_KEYDOWN, WM_CHAR and WM_KEY up messages to write in the window
IntPtr childHandle = FindWindowEx(p[0].MainWindowHandle, IntPtr.Zero,"Edit", IntPtr.Zero);
// WM_KEYDOWN message with parameters for 1st key
SendMessage(childHandle, (uint)0x0100, (UIntPtr)0x00000041, (IntPtr)(0x001E0001));
// WM_CHAR message with parameters for 1st key
SendMessage(childHandle, (uint)0x0102, (UIntPtr)0x00000061, (IntPtr)(0x001E0001));
// WM_KEYDOWN message with parameters for n-th key
SendMessage(childHandle, (uint)0x0100, (UIntPtr)0x00000041, (IntPtr)(0x401E0001));
// WM_CHAR message with parameters for n-th key
SendMessage(childHandle, (uint)0x0102, (UIntPtr)0x00000061, (IntPtr)(0x401E0001));
// WM_KEYUP message
SendMessage(childHandle, (uint)0x0101, (UIntPtr)0x00000041, (IntPtr)(0xC01E0001));
}
コード内のWM_KEYUPSendMessageに到達すると、プログラムがクラッシュし、エラーが発生します。
このエラーを修正するにはどうすればよいですか?WM_KEYUPの前に4つのSendMessageが呼び出され、1つは正常に機能し、2文字の「a」をメモ帳に送信します。
返信ありがとうございます