将焦点切换到另一个应用程序的正确方法(在 .NET 中)
这就是我到目前为止所遇到的:
Dim bProcess = Process.GetProcessesByName("By").FirstOrDefault
If bProcess IsNot Nothing Then
SwitchToThisWindow(bProcess.MainWindowHandle, True)
Else
Process.Start("C:\Program Files\B\B.exe")
End If
它有两个问题。
- 有些人告诉我 SwitchToThisWindow 已被弃用。
- 如果应用程序 B 最小化,则从用户的角度来看,此功能会默默地失败。
那么这样做的正确方法是什么?
This is what I have so far:
Dim bProcess = Process.GetProcessesByName("By").FirstOrDefault
If bProcess IsNot Nothing Then
SwitchToThisWindow(bProcess.MainWindowHandle, True)
Else
Process.Start("C:\Program Files\B\B.exe")
End If
It has two problems.
- Some people have told me that SwitchToThisWindow is deprecated.
- If application B is minimized, this function silently fails from the user's perspective.
So what's the right way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
获取窗口句柄(hwnd),然后使用这个 user32.dll 函数:
VB.net 声明:
C# 声明:
一个考虑因素是,如果窗口最小化,这将不起作用,因此我编写了以下也处理窗口句柄的方法这个案例。这是 C# 代码,将其迁移到 VB 应该相当简单。
使用该代码,就像设置适当的流程变量并调用BringMainWindowToFront(“processName”);一样简单
Get the window handle (hwnd), and then use this user32.dll function:
VB.net declaration:
C# declaration:
One consideration is that this will not work if the window is minimized, so I've written the following method which also handles this case. Here is the C# code, it should be fairly straight forward to migrate this to VB.
Using that code, it would be as simple as setting the appropriate process variables and calling
BringMainWindowToFront("processName");
还有另一种方法,它使用不为人所知的 UI 自动化 API:
大多数情况下,如果可以切换到该窗口,则这将起作用。 Windows 中有很多限制(安全性、UAC、特定配置等),这些限制可能会阻止您改变最终用户的关注点。
There is another way, which uses the not well-known UI Automation API:
Most of the time, this will work if it's possible to switch to that window. There are a lot of limitations in Windows (security, UAC, specific configuration, etc...) that can prevent you to change the end-user focus.
在您的项目中创建一个新类并将以下代码复制粘贴到其中。
现在将以下代码复制粘贴到您所需的区域。
在这里,您调用该函数将焦点转移到其他应用程序的窗口。
Create a New Class in your project and copy-paste the below code in it.
Now copy-paste the below code in your required area.
Here you are calling the function to bring focus to the other application's window.
在 VB.Net 中,您可以使用 AppActivate< /a> 函数。
In VB.Net, you can use the AppActivate function.
我使用 SetForegroundWindow 来显示另一个应用程序的窗口。
如何给予另一个进程焦点来自 C#?
I used SetForegroundWindow to make the window from another application appear.
How can I give another Process focus from C#?
这对我来说工作
This work for me
导入:
将其放入模块中
用法:
来源: http://www.codeproject.com/Tips/232649/Setting-Focus-on-an-External-application#_ rating
Imports:
Put this in a Module
Usage:
Source: http://www.codeproject.com/Tips/232649/Setting-Focus-on-an-External-application#_rating
当我尝试使用从以下代码获取的
exe
以管理员身份运行 SQL Server Management Studio 和 Visual Studio 等应用程序时,以下内容对我有用。切换帮助
客户端
Following worked for me, when I tried for applications like SQL Server Management Studio and Visual Studio, running as Administrator, using the
exe
got from the following code.Toggle Helper
Client