My WPF application has more than one window, I want to attach some of these windows to a window of another process. My problem is that once I attach my window it becomes inv
I'm not sure what you need to do with overlapped windows, but from MSDN:
For compatibility reasons, SetParent does not modify the WS_CHILD or WS_POPUP window styles of the window whose parent is being changed. Therefore, if hWndNewParent is NULL, you should also clear the WS_CHILD bit and set the WS_POPUP style after calling SetParent. Conversely, if hWndNewParent is not NULL and the window was previously a child of the desktop, you should clear the WS_POPUP style and set the WS_CHILD style before calling SetParent.
private void TryToAttach(IntPtr ownerHandle)
{
if(ownerHandle == IntPtr.Zero)
{
return;
}
try
{
var helper = new WindowInteropHelper(window) { Owner = ownerHandle };
}
catch(Exception e)
{
Logger.Error(e, "Could not attach window.");
}
}
I found that I could do this without even using the setParent call. I used HwndSource class as follows:
MyWindow window = new MyWindow();
window.ShowActivated = true;
HwndSourceParameters parameters = new HwndSourceParameters();
parameters.WindowStyle = 0x10000000 | 0x40000000;
parameters.SetPosition(0, 0);
parameters.SetSize((int)window.Width, (int)window.Height);
parameters.ParentWindow = newParent;
parameters.UsesPerPixelOpacity = true;
HwndSource src = new HwndSource(parameters);
src.CompositionTarget.BackgroundColor = Colors.Transparent;
src.RootVisual = (Visual)window.Content;
This is working great now without any problems.