プログラムがバックグラウンドで実行されている場合でも、キーストロークをプログラムに送信したい。しかし、私はこのようなメモ帳に対してのみこれを行うことができます、
[DllImport("user32.dll")]
protected static extern byte VkKeyScan(char ch);
[DllImport("user32.dll", SetLastError = true)]
protected static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("User32.Dll", EntryPoint = "PostMessageA")]
protected static extern bool PostMessage(IntPtr hWnd, uint msg, int wParam, int lParam);
char Key = // key value to send
IntPtr hWnd = FindWindowEx(_handle, IntPtr.Zero, "edit", null); // _handle is the windows handle of the program (here its notepad)
PostMessage(hWnd, WM_KEYDOWN, VkKeyScan(Key), 0);
しかし、他のすべてのアプリケーションでは、バックグラウンドでキーストロークを送信することはできません。そのプログラムの名前がわからないのでlpszClass
(これはそのプログラムの入力領域のuserControl名だと思います。メモ帳の場合はそうです"edit"
。このサーフィンインターネットを見つけました)。
私が行っている他のすべてのアプリケーションの場合、アプリケーションをフォアグラウンドに移動し、キーを送信してから、プログラムをフォアグラウンドに戻します。プログラムを常にフォアグラウンドで実行する必要があります。
[DllImport("USER32.DLL")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
SetForegroundWindow(_handle); // _handle is the windows handle of the program
System.Threading.Thread.Sleep(50); // Waiting few milliseconds till application coming to foreground.
wsh.SendKeys(Key.ToString(), ref wait); // wsh is WshShellClass wsh= new WshShellClass();
SetForegroundWindow(_mainHandle); // _mainHandle is the windows handle of my application
しかし、この方法は機能していません。いくつかのキーが見落とされ、プログラムの前景->背景->前景->背景......ダンスのように...
バックグラウンドで実行されている場合に他のアプリケーションにキーを送信する方法。lpszClass
または、プログラムを見つける方法/ソースはありますか?
必要な情報を見逃してしまった場合は申し訳ありません。これは大きなアプリケーションです。ここに必要な部品だけを掲載しました。誰かが追加情報を必要とする場合、plsは尋ねます。