简体   繁体   English

关闭WPF模式窗口后如何将MFC对话框置于前台

[英]How do I bring an MFC dialog to foreground after closing WPF modal window

I have a MFC app that posts a message through a COM interface to open a form on another process both in C++. 我有一个MFC应用程序,该应用程序通过COM界面发布消息,以在C ++中的另一个进程上打开表单。 That process in turn is using an ActiveX control to pass the message on to open either a WinForm or WPF window (.NET). 该过程依次使用ActiveX控件传递消息以打开WinForm或WPF窗口(.NET)。 I am passing over the HWND of my original MFC dialog in order to have the new dialog become a child. 我正在通过原始MFC对话框的HWND,以使新对话框成为子对话框。

Everything up to this point is working, though the WPF form required me to disable the parent in addition to setting the owner via a WindowInteropHelper and of course re-enable the parent on closing. 到目前为止,一切工作正常,尽管WPF表单要求我除了通过WindowInteropHelper设置所有者外,还要禁用父级,并且当然在关闭时重新启用父级。

The problem I'm having occurs when I close the form. 关闭表单时出现了我遇到的问题。 When I close the WinForm, the original dialog becomes active, but when I close the WPF form any window that had focus at any point earlier becomes active. 当我关闭WinForm时,原始对话框将变为活动状态,但是当我关闭WPF窗体时,任何在较早位置聚焦的窗口将变为活动状态。 The only way I can reactivate my MFC dialog is by clicking on the title bar. 重新激活MFC对话框的唯一方法是单击标题栏。

I have tried a dozen ways of trying to call SetForegroundWindow and checked to make sure that my process is the current active process. 我尝试了多种尝试调用SetForegroundWindow的方法,并检查以确保我的进程是当前活动的进程。 If I put a break point in and continue, then my main dialog becomes active properly. 如果我输入一个断点并继续,则我的主对话框将正确激活。

So I have solved this issue for now by converting the WPF Window into a UserControl and hosting the control in a (Win) Form. 因此,我现在已经通过将WPF窗口转换为UserControl并将该控件托管在(Win)窗体中​​来解决了此问题。

The only downside is that this UserControl is also used from my WPF app and so I also have a WPF Window which contains the UserControl. 唯一的缺点是,我的WPF应用程序中也使用了此UserControl,因此我也有一个包含UserControl的WPF窗口。 I simply created a plain Form that takes the control in the constructor and then setup the look and feel. 我只是创建了一个简单的Form,该Form接受构造函数中的控件,然后设置外观。 This also meant that I was able to get rid of the code for setting the owner and handling the owner's enable state for the WPF Window. 这也意味着我可以摆脱用于设置所有者和处理WPF窗口的所有者启用状态的代码。

So my code for opening the window went from: 所以我打开窗口的代码来自:

MyWindow window = new MyWindow();
WindowInteropHelper helper = new WindowInteropHelper(window);
helper.Owner = owner;
EnableWindow(owner, false); // Platform Invoke
window.Closed += (s, e) => { EnableWindow(owner, true); };
window.ShowDialog();

and became: 成为:

HostWinForm WinForm = new HostWinForm(new MyControl());
winForm.ShowDialog();

I don't know if there is a solution that would allow me to keep one WPF Window and have my modal behavior too, but this solution works for me. 我不知道是否有一种解决方案可以让我保留一个WPF窗口并具有模态行为,但是此解决方案对我有用。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM