Sendkeys仅在首先激活窗口时才起作用

时间:2017-11-13 14:50:54

标签: vb.net google-chrome sendkeys

当我打开一个窗口时,代码可以完美无缺。但是当我有更多实例运行时,我需要先激活窗口。好像SetForegroundWindow(p.MainWindowHandle)似乎无法正常工作?

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

For Each p As System.Diagnostics.Process In System.Diagnostics.Process.GetProcesses()
    If p.ProcessName = "chrome" AndAlso p.MainWindowTitle.Contains("game") AndAlso p.MainWindowHandle <> IntPtr.Zero Then
        SetForegroundWindow(p.MainWindowHandle)
        SendKeys.SendWait("{F2}")
    End If
Next

End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
For Each p As System.Diagnostics.Process In System.Diagnostics.Process.GetProcesses()
    If p.ProcessName = "chrome" AndAlso p.MainWindowTitle.Contains("chat") AndAlso p.MainWindowHandle <> IntPtr.Zero Then
        SetForegroundWindow(p.MainWindowHandle)
        SendKeys.SendWait("{F2}")
    End If
Next
End Sub

有什么想法吗?

0 个答案:

没有答案