[英]How Can My Win32 App Steal Focus From My UWP App?
I've tried the following code: 我尝试了以下代码:
[DllImport("user32.dll")]
private static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern IntPtr SetFocus(IntPtr hwnd);
void TakeFocus()
{
var process = Process.GetProcessesByName("myProcess").FirstOrDefault();
if (process != null)
{
// Tried each of the following:
ShowWindow(process.MainWindowHandle, 1);
ShowWindow(process.MainWindowHandle, 3);
ShowWindow(process.MainWindowHandle, 9);
ShowWindow(process.MainWindowHandle, 5);
SetFocus(process.MainWindowHandle);
SetForegroundWindow(process.MainWindowHandle);
}
}
I have a WPF companion app which runs in the background while a UWP app is running in the foreground. 我有一个WPF伴侣应用程序,它在后台运行,而UWP应用程序在前台运行。 They communicate via WebSocket.
他们通过WebSocket进行通信。 I'm trying to create a method in the WPF app so it (or any other window) can steal focus from an activated UWP app, sending it into suspended state.
我正在尝试在WPF应用程序中创建一个方法,以便它(或任何其他窗口)可以从已激活的UWP应用程序中窃取焦点,并将其发送到暂停状态。 Nothing I try seems to work, and there's no way to programatically make a UWP app suspend itself AFAIK without using the Launcher class (not an option for me, unless there's a way to call it without actually launching something-I haven't been able to do this).
我尝试执行的所有操作似乎都没有任何作用,并且无法以编程方式使UWP应用在不使用Launcher类的情况下自行挂起AFAIK(这对我来说不是一个选择,除非有一种方法可以在不实际启动的情况下调用它-我无法去做这个)。 Normally I would assume it can't be done but I've seen programs that do it.
通常,我认为这是无法完成的,但是我看过能做到这一点的程序。 Steam Big Picture Mode, for example, will steal focus from a UWP app when it is launched from a background process.
例如,当从后台进程启动时,Steam大图片模式会从UWP应用中夺走焦点。
The supported way of suspending a UWP programmatically is available in the Spring 2018 update for Windows 10. It's already available in Insider builds/SDKs. Windows 10的2018年春季更新中提供了以编程方式挂起UWP的受支持方式。Insider内部版本/ SDK已提供该方式。 This is the API to call:
这是要调用的API:
https://docs.microsoft.com/en-us/uwp/api/windows.system.appresourcegroupinfo.startsuspendasync#Windows_System_AppResourceGroupInfo_StartSuspendAsync https://docs.microsoft.com/zh-cn/uwp/api/windows.system.appresourcegroupinfo.startsuspendasync#Windows_System_AppResourceGroupInfo_StartSuspendAsync
IList<AppDiagnosticInfo> infos = await AppDiagnosticInfo.RequestInfoForAppAsync();
IList<AppResourceGroupInfo> resourceInfos = infos[0].GetResourceGroups();
await resourceInfos[0].StartSuspendAsync();
Here is a trivial sample app: https://1drv.ms/u/s!AovTwKUMywTNoYQ3PrmBfZIGXmbULA 这是一个简单的示例应用程序: https : //1drv.ms/u/s!AovTwKUMywTNoYQ3PrmBfZIGXmbULA
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.