Vb.net 如何基于进程名VB设置其他应用程序的焦点

Vb.net 如何基于进程名VB设置其他应用程序的焦点,vb.net,process,focus,Vb.net,Process,Focus,如何在VB2010中基于进程名称设置其他应用程序的焦点 我现在可以做的是使用FindWindow将焦点设置在基于windows名称的其他应用程序上,然后使用setForeGroundIndow。下面是我目前拥有的 Dim theHandle As IntPtr theHandle = FindWindow(Nothing, "Gmail: Email from Google") If theHandle <> IntPtr.Zero T

如何在VB2010中基于进程名称设置其他应用程序的焦点

我现在可以做的是使用FindWindow将焦点设置在基于windows名称的其他应用程序上,然后使用setForeGroundIndow。下面是我目前拥有的

        Dim theHandle As IntPtr
        theHandle = FindWindow(Nothing, "Gmail: Email from Google")
        If theHandle <> IntPtr.Zero Then
        SetForegroundWindow(theHandle)
将手柄调暗为IntPtr
theHandle=FindWindow(无,“Gmail:Email from Google”)
如果他们处理IntPtr.Zero,那么
SetForegroundWindow(手柄)
问题是FindWindow需要确切的windows名称才能工作,而我并不总是知道确切的名称。(因为我的程序打开了用户进入的不同网站,所以我无法控制他们打开的网站)。那么,我是否可以用进程名来设置焦点呢?(在本例中为firefox.exe)欢迎提出任何其他建议


谢谢

您可以使用
System.Diagnostics.Process
按名称查找流程,然后找到窗口标题:

For Each app As Process In Process.GetProcessesByName("firefox")
    Dim theHandle As IntPtr = FindWindow(Nothing, app.MainWindowTitle)
    If theHandle <> IntPtr.Zero Then
        SetForegroundWindow(theHandle)
    End If
Next
作为进程中的进程用于每个应用程序。GetProcessesByName(“firefox”)
将手柄调暗为IntPtr=FindWindow(无,app.main窗口标题)
如果他们处理IntPtr.Zero,那么
SetForegroundWindow(手柄)
如果结束
下一个

使用静态
GetProcessByName
方法,然后使用
MainWindowTitle
属性。对于此示例,您需要导入System.Diagnostics以导入正确的命名空间。

您可以使用
System.Diagnostics.Process
按名称查找进程,然后找到窗口标题:

For Each app As Process In Process.GetProcessesByName("firefox")
    Dim theHandle As IntPtr = FindWindow(Nothing, app.MainWindowTitle)
    If theHandle <> IntPtr.Zero Then
        SetForegroundWindow(theHandle)
    End If
Next
Private Sub ActivateApp(ByVal pID As Integer)
        Dim p As Process = Process.GetProcessById(pID)
        If p IsNot Nothing Then
            SetForegroundWindow(p.MainWindowHandle)
        End If
    End Sub
作为进程中的进程用于每个应用程序。GetProcessesByName(“firefox”)
将手柄调暗为IntPtr=FindWindow(无,app.main窗口标题)
如果他们处理IntPtr.Zero,那么
SetForegroundWindow(手柄)
如果结束
下一个
使用静态
GetProcessByName
方法,然后使用
MainWindowTitle
属性。对于此示例,您需要
导入System.Diagnostics
才能导入正确的命名空间

Private Sub ActivateApp(ByVal pID As Integer)
        Dim p As Process = Process.GetProcessById(pID)
        If p IsNot Nothing Then
            SetForegroundWindow(p.MainWindowHandle)
        End If
    End Sub
然后用这个:

ActivateApp(System.Diagnostics.Process.GetCurrentProcess.Id)
SendKeys.SendWait("~")
然后用这个:

ActivateApp(System.Diagnostics.Process.GetCurrentProcess.Id)
SendKeys.SendWait("~")

如果语法关闭,请原谅;我没有在这台电脑上安装VB Express。非常脆弱;最好希望他们不要改名字!这已经发生过一次了。@Cody我不能不同意。然而,OP询问如何根据进程名称设置焦点。如果关闭,请原谅语法;我没有在这台电脑上安装VB Express。非常脆弱;最好希望他们不要改名字!这已经发生过一次了。@Cody我不能不同意。然而,OP询问如何根据进程名称设置焦点。