I work on add-in for CAD programm. And I've found a bug in owner logic. When I create a simple WPF window I get the handle of the main CAD window and set it as the owner of my WPF window. The handle is correct, I checked it by SPY++ Here how I do that in constructor of WPF window (it has type System.Windows.Window):
var helper = new WindowInteropHelper(this);
helper.Owner = owner;
MSDN says the next tips about Owner-Child relationships:
- If an owner window is minimized, all its owned windows are minimized as well.
- If an owned window is minimized, its owner is not minimized.
- If an owner window is maximized, both the owner window and its owned windows are restored.
- An owner window can never cover an owned window.
- Owned windows that were not opened using ShowDialog are not modal. The user can still interact with the owner window.
- If you close an owner window, its owned windows are also closed.
- If an owned window was opened by its owner window using Show, and the owner window is closed, the owned window's Closing event is not raised.
And all work except "An owner window can never cover an owned window". If my WPF window is non-modal it can be hidden behind the main CAD window when I click on it. If my WPF window is modal in some cases it also can be hidden behind the main CAD window when I minimize and maximize it.
So I've used TOPMOST property to fix these problems. And I set it as TRUE when try to click on the main CAD window and set as FALSE when try to switch to other programms. It looks next:
private IntPtr HookForCAD(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == WM_ACTIVATE && (int)wParam == WA_INACTIVE)
{
this.Topmost = lParam == new WindowInteropHelper(this).Owner;
}
if (msg == WM_ACTIVATEAPP)
{
this.Topmost = (int)wParam != WA_INACTIVE;
}
return IntPtr.Zero;
}
It helps fix almost all cases. But there is still one issue. When we see the part of the window of other program (3) behind the CAD window (2). We open our WPF window (1) and try to switch to other program by mouse click on area of window (3). And it doesn't work. Window (3) will be activated but we still see the WPF window and CAD window in the foreground.
How can I fix it ??
