简体   繁体   中英

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++. That process in turn is using an ActiveX control to pass the message on to open either a WinForm or WPF window (.NET). I am passing over the HWND of my original MFC dialog in order to have the new dialog become a child.

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.

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. The only way I can reactivate my MFC dialog is by clicking on the title bar.

I have tried a dozen ways of trying to call SetForegroundWindow and checked to make sure that my process is the current active process. 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.

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. I simply created a plain Form that takes the control in the constructor and then setup the look and feel. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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