我的 Win32 应用程序如何从我的 UWP 应用程序窃取焦点? [英] How Can My Win32 App Steal Focus From My UWP App?

查看:33
本文介绍了我的 Win32 应用程序如何从我的 UWP 应用程序窃取焦点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了以下代码:

[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);
    }
}

我有一个 WPF 配套应用程序,它在后台运行,而 UWP 应用程序在前台运行.它们通过 WebSocket 进行通信.我正在尝试在 WPF 应用程序中创建一个方法,以便它(或任何其他窗口)可以从激活的 UWP 应用程序中窃取焦点,将其发送到挂起状态.我尝试的任何东西似乎都不起作用,并且没有办法在不使用 Launcher 类的情况下以编程方式使 UWP 应用程序挂起自身去做这个).通常我会假设它无法完成,但我见过这样做的程序.例如,Steam 大图片模式会在从后台进程启动时从 UWP 应用窃取焦点.

I have a WPF companion app which runs in the background while a UWP app is running in the foreground. They communicate via 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. 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). 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.

推荐答案

支持的以编程方式暂停 UWP 的方式在适用于 Windows 10 的 Spring 2018 更新中可用.它已在 Insider 版本/SDK 中可用.这是要调用的 API:

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. This is the API to call:

https://docs.microsoft.com/en-us/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();

这是一个简单的示例应用程序:https://1drv.ms/u/s!AovTw​​KUMywTNoYQ3PrmBfZIGXmbULA

Here is a trivial sample app: https://1drv.ms/u/s!AovTwKUMywTNoYQ3PrmBfZIGXmbULA

这篇关于我的 Win32 应用程序如何从我的 UWP 应用程序窃取焦点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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