【问题标题】:How to make a WPF Window behave as DialogWindow when launched through Process class通过 Process 类启动时如何使 WPF 窗口表现为 DialogWindow
【发布时间】:2021-05-05 13:21:26
【问题描述】:

我有一个小型 WPF 应用程序,比如 SubWPF ,它最初显示一个允许用户浏览文件的主窗口。我在单击另一个应用程序(例如 MainWPF)的按钮时启动此 wpf 应用程序。简而言之,当我点击“Launch Sub”时,SubWPF.exe 通过 Process 类从 MainWPF 启动,如下所示。

Process p = new Process();
p.StartInfo.FileName = "SubWPF.exe";
       
p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
p.StartInfo.UseShellExecute = false;
p.StartInfo.Verb = "runas";
p.Start();

问题出在后台我有 MainWPF 窗口。我需要 subwpf 始终位于顶部并防止用户单击 mainwpf 的背景按钮。准确地说,需要实现 showdialog() 的行为。我不能在我的 subwpf 中使用TopMost=true,因为一旦用户在主窗口中选择了一个文件,这个窗口就会关闭并显示另一个显示文件内容的窗口。设置TopMost=true 不允许我关闭文件选择窗口。

【问题讨论】:

  • 将 MainWPF 的 IsHitTestVisible 设置为 false 并恢复 byProcess.Exited 事件
  • 还有其他选择吗?由于 MainWPF 是另一个团队实现的主要应用程序,我很难对其进行更改。

标签: c# wpf mvvm


【解决方案1】:

您将无法通过此答案达到确切的模式,但是如果您将子窗口最大化到父窗口的大小,您将能够阻止主窗口进行输入(现在就知道了 - 您可以制作透明背景并将您的实际业务控制放在子窗口的中心)。
另一个陷阱是调整主窗口大小的可能性 - 要绕过它,您可以尝试临时更改主窗口样式:Modify the windows style of another application using winAPI.
除了最大化您可以捕获鼠标并在每次单击时将子窗口移动到顶部,或致电开发团队并按照 Leonid Malyshev 在评论中提供的操作。 因此,您需要为子窗口的子窗口结束集WindowStyle="None"Window.Loaded 事件处理程序添加一些逻辑:

using System.Runtime.InteropServices;

[DllImport("user32.dll")]
public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

[DllImport("user32.dll")]
public static extern IntPtr SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);

private void SubWindow_Loaded(object sender, RoutedEventArgs e)
{
    var p = Process.GetProcessesByName("YourMainWindow")[0];

    SetParent(new WindowInteropHelper(this).Handle, p.MainWindowHandle);

    WindowState = WindowState.Maximized; //Does maximize sub window to the size of parent window.

    IntPtr HWND_TOPMOST = new IntPtr(-1);
    const short SWP_NOACTIVATE = 0x0010;

    SetWindowPos(new WindowInteropHelper(this).Handle, p.MainWindowHandle, 0, 0, (int)Width, (int)Height, SWP_NOACTIVATE);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-25
    • 2011-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多