在平板模式下启动另一个应用程序并置于最顶层

4
当我从我的应用程序运行另一个 .exe 文件时,它会在后台启动并不会显示在屏幕顶部的应用程序,而是显示平板模式主屏幕。 此功能在普通桌面模式下工作正常,但在 Windows 10 平板模式下运行时,它不会显示在屏幕顶部,而是在后台启动。
我已经使用了 myWindow.TopMost = true;,但在 Windows 10 平板模式下没有按预期工作。
用于启动 exe 文件的代码:
Process p = new Process();
p.StartInfo.RedirectStandardOutput= true;
p.RedirectStandardInput = true;
p = Process.Start("myApp.exe");
p.WaitForExit();

我正在启动的exe应用程序是我的自己的应用程序(不是系统应用程序),我在Windows 10上运行该应用程序。

只有在平板电脑模式下它才无法正常工作(而且我仅针对平板电脑定位我的应用程序)。

任何帮助都将不胜感激..!


你应该设置处理你的应用程序的父窗口。如果不设置,TopMost = true 将无法帮助你。 - mihai
2个回答

3
作为一个面对类似情况的人,(和平板电脑或Windows 10无关。只有WPF和TopMost标签相似),我将向您展示如何解决它:我希望FilterWindow始终是最顶层的(但仅覆盖我的应用程序,而不是整个操作系统中的所有应用程序)。
请参考我的代码,这可能会对您有所帮助。
private void OnFilter() {   
    var filterViewModel = ViewModelLocator.FilterViewModel;

    /* ... */

    var filterWindow = new FilterWindow {
        DataContext = filterViewModel,
        Owner = GetParentWindow()
    };
    filterWindow.ShowDialog();
    SelectedIndex = 0;
}

private static Window GetParentWindow() {
    Window parent = null;

    var activeWindows = Application.Current.Windows.Cast<Window>().Where(item => (item).IsActive).ToList();
    if (activeWindows.Any()) {
    parent = activeWindows[activeWindows.Count - 1];
    }
    else {
        foreach (var item in 
            Application.Current.Windows.Cast<object>().Where(item => item.GetType().Name == typeof(RibbonWindow).Name)) {
            parent = item as Window;
        }
    }
    return parent;
}

魔法在于 Owner = GetParentWindow()。如果不设置 OwnerFilterWindow 的行为会非常荒谬。
希望这能对你有所帮助。如果没有,我将删除这个回复。(它不能适应评论)

谢谢您的回复,但我应该把这段代码写在哪里呢?因为我正在启动一个新的exe(应用程序)。就像我发布的调用Process.Start()方法来运行exe的代码一样...?? - Rahul Shirphule
@RahulShirphule,对于进程,你应该使用WinApi中的窗口函数。SetParent应该可以解决问题(当你启动EXE时)。更多信息请参见:https://msdn.microsoft.com/en-us/library/windows/desktop/ms633541(v=vs.85).aspx。此外,这对你也可能很有趣:http://stackoverflow.com/questions/8312535/can-you-launch-a-process-on-top-of-the-topmost-window-csharp-wpf - mihai
是的,我看过这个链接了。但是什么时候调用setParent方法没有提到,也就是在p = Process.Start("myApp.exe");之前还是之后?我尝试了两种方式都没有成功...你有什么想法吗?请告诉我。 - Rahul Shirphule
@RahulShirphule,我在这里看到(附加网址),SetParent在进程启动后被调用。https://dev59.com/IWPVa4cB1Zd3GeqP5mwn。此外,在这里:https://dev59.com/I2_Xa4cB1Zd3GeqP3J0d。 - mihai

0

Moerfi的解决方案使用Owner = GetParentWindow()非常出色,非常感谢这个解决方案。它还解决了我遇到的另一个问题。

我正在为在Windows 10 Pro上以平板模式运行的Surface 3编写应用程序,每当关闭MessageBox或自定义对话框控件框时,与返回到父窗口相反,Win 10会转到开始菜单。

好像一旦打开对话框控件,父窗口就被放置到后台,因此当对话框控件关闭时,没有活动窗口供Win 10切换回来。

在子对话框控件上设置所有者解决了这个问题。非常感谢。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接