0

I have two applications. One is a WinForms application (aka Parent) and the other is a WPF application (Child).

Child can run independently as can Parent. I'm working on functionality for Child to behave like a UserControl from within Parent but still run as a separate process.

I've had success calling SetParent and having a Forms.UserControl handle the sizing and position of Child.

//owningHandle is the handle of the winform control that Child resides in.
//owner is the user control
//addinHandle is the handle of the Child window

SetParent(addinHandle, owningHandle);
SetWindowLongPtr(addinHandle, GWL_STYLE, style);
SetWindowPos(addinHandle,
             IntPtr.Zero,
             owner.Left, owner.Top,
             (int) owner.Width, (int)owner.Height,
             SetWindowPosFlags.SWP_NOZORDER | SetWindowPosFlags.SWP_NOACTIVATE);

Now we want to be able to UnSetParent the Child process from the Parent. I've read that "all" I need to do is call SetParent with the parent parameter being 0 (IntPtr.Zero).

//I store the original Window style in _originalStyle before calling the SetParent logic above. This way we can get the proper Windows border around the application
uint newStyle = (uint)_originalStyle;
newStyle = (newStyle | (uint)WindowStyles.WS_POPUP) & (uint)(~WindowStyles.WS_CHILD);
SetWindowLongPtr(addinHandle, GWL_STYLE, newStyle);
SetParent(addinHandle, IntPtr.Zero);

However, I'm noticing that the majority of the time my Child application can't be clicked on in any way. None of the UI components respond to clicks including the minimize,restore,close buttons on the window. I get the Windows beep noise if I click anywhere. I can attach a debugger and see that any of the timers running in the application are still going.

Is there some step I need to take when detaching a WPF application?

Boumbles
  • 2,473
  • 3
  • 24
  • 42
  • Usage of SetParent() like this was supported to make it easier to port Windows 3.x programs. A WPF app is not *anything* like a 3.x program so random misery is expected. You might get somewhere by *not* turning on the WS_CHILD style bit. And don't randomly turn on WS_POPUP. – Hans Passant Jun 15 '16 at 14:32
  • That line is actually something I added in this morning after some hunting on SO. If it's not something I should be doing I will for sure remove it. – Boumbles Jun 15 '16 at 15:08

0 Answers0