0
votes

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.

1
If I look at the 'Related' items next to your question, I get interesting suggestions like: stackoverflow.com/questions/273276/…Pieter21
I would try using snoop to see the events that occur..makc
snoop is causing a stackoverflow exception, I hooked up register message and on close I post a message back to my dialog and I added a call ::SetActiveWindow(GetTopLevelParent()->GetSafeHwnd()); and I see flicker from the random other window back to my MFC dialogtdop

1 Answers

0
votes

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.