首页 文章

在模态模式下显示来自其他进程的WPF窗口

提问于
浏览
2

我有两个WPF应用程序和一个进程管理器,它将数据从第一个WPF应用程序传递到第二个WPF应用程序,反之亦然 . 在一个用例中,我必须在模态模式下在第二个应用程序的窗口(主窗口)上显示第一个应用程序的窗口(主窗口) . 因此,将禁用第二个WPF应用程序的窗口,并在第一个WPF应用程序的窗口顶部显示 . 所需行为与在单个WPF应用程序中以模式模式显示窗口相同 . 知道如何从另一个WPF应用程序访问一个WPF应用程序的窗口?

在Winform应用程序的情况下,我们通过将Window Handle(intPtr)传递给另一个应用程序来完成它,同时以模态模式显示窗口使用句柄,如:

System.Windows.Forms.Form.ShowDialog(System.Windows.Forms.IWin32Window)

在WPF应用程序的情况下可以实现类似的功能?提前致谢 .

2 回答

  • 4

    ===========================开始更新===================== ================

    代码:

    using System.Windows; // Window, WindowStartupLocation
    using System.Windows.Interop; // WindowInteropHelper
    using System.Runtime.InteropServices; // DllImport
    ...
    
    // Instantiate the owned WPF window
    CenteredWindow cw = new CenteredWindow();
    // Get the handle to the non-WPF owner window
    IntPtr hWnd = ...
    
    CenteredWindow cw = new CenteredWindow();
    
    EnableWindow(hWnd, false); // disable parent window
    try
    {
        // Set the owned WPF window’s owner with the non-WPF owner window
        WindowInteropHelper helper = new WindowInteropHelper(cw);
        helper.Owner = hWnd;
        cw.ShowDialog();
    }
    finally
    {
        EnableWindow(hWnd, true); // enable parent window
    }
    
    ...
    [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    private static extern bool EnableWindow(IntPtr hwnd, bool enable);
    

    在@ kamal-nayan评论的MS Connect Link的帮助下,我修改了上面的代码,它对我很有用 .

    关键是 disable 父窗口,当关闭模态对话框时, enable 父窗口 .

    =========================== END UPDATE ===================== ================

    using System.Windows; // Window, WindowStartupLocation
    using System.Windows.Interop; // WindowInteropHelper
    ...
    // Instantiate the owned WPF window
    CenteredWindow cw = new CenteredWindow();
    
    // Get the handle to the non-WPF owner window
    IntPtr ownerWindowHandle = ...; // Get hWnd for non-WPF window
    
    // Set the owned WPF window’s owner with the non-WPF owner window
    WindowInteropHelper helper = new WindowInteropHelper(cw);
    helper.Owner = ownerWindowHandle;
    cw.ShowDialog();
    

    这是我找到的唯一解决方案 . 它不是真正的Modal,即你仍然可以激活父级,但好处是子窗口仍然在父级之上 .

    http://blogs.msdn.com/b/wpfsdk/archive/2007/04/03/centering-wpf-windows-with-wpf-and-non-wpf-owner-windows.aspx

  • 0
    _parameters = new HwndSourceParameters("myWindow");
    
    _parameters.WindowStyle = WindowStyles.WS_SYSMENU | WindowStyles.WS_VISIBLE | WindowStyles.WS_CAPTION | WindowStyles.WS_CHILD | WindowStyles.WS_POPUP;
    _parameters.SetPosition(50, 50);
    
    _parameters.ParentWindow = ParentWindowHandle;           
    _hwndSource = new HwndSource(_parameters);
    
    _hwndSource.SizeToContent = SizeToContent.WidthAndHeight;
    _hwndSource.RootVisual = modalWindowContent;
    

    这就是我能够从一个进程显示窗口作为窗口从其他进程显示的方式 .

相关问题