如何基于进程名称VB设置对其他应用程序的关注 [英] How to set focus on other application based on process name VB

查看:51
本文介绍了如何基于进程名称VB设置对其他应用程序的关注的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何基于VB2010中的进程名称将重点放在其他应用程序上?

How can I set focus on other application based on process name in VB2010?

我现在能做的是使用FindWindow然后基于Windows名称将焦点集中在其他应用程序上,然后使用SetForegroundWindow.以下是我目前拥有的

What I can do now is set focus on other application based on windows name using FindWindow then use SetForegroundWindow. Below is what I currently have

        Dim theHandle As IntPtr
        theHandle = FindWindow(Nothing, "Gmail: Email from Google")
        If theHandle <> IntPtr.Zero Then
        SetForegroundWindow(theHandle)

问题是FindWindow需要确切的Windows名称才能工作,但我并不总是知道确切的名称.(因为我的程序打开了用户输入的其他网站,所以我无法控制他们打开的网站).因此,无论如何,我可以使用流程名称来设置焦点吗?(在这种情况下为firefox.exe)欢迎其他任何建议.

The problem is that FindWindow need exact windows name to works and I don't always know the exact name. (Because my program open up different website that the user enter, so I have no control over they site they open). So is there anyway that I can set focus using the process name instead? (in this case firefox.exe) Any other suggestions are welcome.

谢谢

推荐答案

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

You can use System.Diagnostics.Process to look up a process by name and then find the window title:

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 方法,然后使用 MainWindowTitle 属性.对于此示例,您需要 Import System.Diagnostics 导入正确的名称空间.

Use the static GetProcessesByName method and then the MainWindowTitle property. For this sample you would need Import System.Diagnostics to import the right namespace.

这篇关于如何基于进程名称VB设置对其他应用程序的关注的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆