In the WPF application we are hosting third party application(Win32) using WindowsFormsHost control by setting a parent for the main window of that third party application. Sample Code:
<telerikDocking:RadPaneGroup>
<telerikDocking:RadDocumentPane>
<Grid>
<WindowsFormsHost x:Name="windowFormHost">
<wf:Panel x:Name="TestPanel"/>
</WindowsFormsHost>
</Grid>
</telerikDocking:RadDocumentPane>
</telerikDocking:RadPaneGroup>
On loading of this control, the Panel will be set as a parent for the Main window of the third party application. This is done using Windows Native Methods.
public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
Parent handle is the TestPanel.Handle and the child handle is Process.MainWindowHandle of the third party application.
Issue 1: Now when any action is performed from the WPF application window on the third party application (using the api's provided by it), it will show a progress bar which is a modal dialog to it. When this happens, the focus is shifted to that progress dialog and after the progress dialog is closed, the WPF window goes behind the previously activated window. It could be another window of the WPF application or could be even a notepad window.
As per msdn https://learn.microsoft.com/en-us/dotnet/api/system.windows.window.showdialog?redirectedfrom=MSDN&view=netframework-4.8#System_Windows_Window_ShowDialog. "When a modal WPF window (a window opened by calling ShowDialog) is closed, the previously activated window is reactivated. If a modal WPF window has an owner window (see Owner), the owner window is not reactivated when the modal WPF window is closed unless it was the previously activated window."
Workaround: Hooking on to the third party application windows and if it launches any child windows, set the focus on that window so that it becomes foreground window.
Any other better way of handling this?
Issue 2: It gets even more complicated if the WPF window (which is currently hosting the third party window) shows a modal window and any action on this modal window results in a child window of the third party main window. If the child window appears and disappears even before it could be processed(to set the focus on it and make it foreground), then the first workaround doesn't work.
Need inputs to handle this gracefully or in any better way possible.