简体   繁体   English

我的Win32 App如何从UWP App窃取焦点?

[英]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!AovTw​​KUMywTNoYQ3PrmBfZIGXmbULA

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 UWP 应用程序中使用命令行 arguments 实现启动 win32 exe? - How do you implement Launching a win32 exe with command line arguments from an UWP app? 将文件从我的应用程序拖放到另一个应用程序的Win32 API过程 - The Win32 API procedure of drag-and-dropping a file from my app to another app 我可以从 UWP 应用程序的进程外后台任务中启动 FullTrust Win32 应用程序吗? - Can I launch a FullTrust Win32 app from within a UWP app's out-of-process background task? 从主UWP进程终止UWP应用的Win32进程,或向其发送消息 - Terminate the win32 process of UWP app from main UWP process, or send message to it 如何通过后台 Win32 进程使用全局快捷方式启动 UWP 应用程序? - How to start a UWP app with a global shortcut through a background Win32 process? 如何防止菜单(ContextMenuStrip)窃取我的TextBox控件的焦点? - How to prevent menu (ContextMenuStrip) to steal focus from my TextBox control? 在启动时从Metro应用程序进入Win32桌面 - enter win32 desktop from metro app at its startup 内存映射文件是否作为 uwp 应用程序和 win32 进程之间的 ipc 支持 - Is Memory-mapped-files as ipc between uwp app and win32 process supported 如何找到父母非托管win32应用程序的子项 - How to find a child of a parent unmanaged win32 app 如何在Win32应用程序中托管ac#控件? - How can I host a c# control in a win32 app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM