现在,我正在使用PostMessage和MouseClick将按键发送到应用程序。我要发送的是KeyPress,完成按键后(KeyUp),然后发送鼠标单击。
代码
const UInt32 WM_KEYDOWN = 0x0100;
const UInt32 WM_KEYDOWN = 0x0101;
const int VK_F1 = 0x70;
const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
const uint MOUSEEVENTF_LEFTUP = 0x0004;
[DllImport("user32.dll")]
static extern bool PostMessage(IntPtr hWnd, UInt32 Msg, int wParam, int lParam);
[DllImport("user32.dll")]
static extern void mouse_event(uint dwFlags, int dx, int dy, uint dwData, UIntPtr dwExtraInfo);
private void Timer1_Tick(object sender, EventArgs e)
{
Process processes = Process.GetProcessById(_processID);
PostMessage(processes.MainWindowHandle, WM_KEYDOWN, VK_F1, 0);
Thread.Sleep(10);
PostMessage(processes.MainWindowHandle, WM_UP, VK_F1, 0);
Thread.Sleep(10);
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, UIntPtr.Zero);
Thread.Sleep(10);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, UIntPtr.Zero);
Thread.Sleep(10);
}
此代码有效,但仅在较高的计时器间隔内。当我将计时器减少到100或小于100时,这就像冻结鼠标单击,不再执行任何按键操作。我想知道的是如何使它在较低的计时器间隔内正常工作。还是有更好的解决方案来完成它。
谢谢。