Resizing a WPF window that is a child to a CView
I am facing a weird problem and I need your help. I have an MFC MDI application, and I am trying to create a WPF Window as a child of the opened CView.
I was able to successfully do that by handling the OnCreate message in my CView, and creating the WPF window. I also set the CView to be the parent of the WPF window so it behaves as its child.
Here is what I did:
int CMyView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (C开发者_开发百科View::OnCreate(lpCreateStruct) == -1)
return -1;
m_windowWrapper.Create(this->GetSafeHwnd());
return 0;
}
void CMyWindowWrapper::Create(HWND hParent)
{
m_myWindow = gcnew MyWindow();
m_myWindow->Show();
IntPtr^ hChildWnd = m_myWindow->GetHwnd();
::SetParent((HWND)hChildWnd->ToPointer(), hParent);
}
public partial class MyWindow : Window
{
public MyWindow()
{
InitializeComponent();
MouseLeftButtonDown += (o, e) => DragMove();
}
public IntPtr GetHwnd()
{
return (new System.Windows.Interop.WindowInteropHelper(this)).Handle;
}
}
The WPF window appears as expected. Now the problem happens when I try to resize the WPF window. Suddenly the controls inside the window are streched and re-positioned as if the new size of the WPF window is the size of the parent CView! I tried to handle the SizeChanged event, and found that the NewSize was much larger than the size of the WPF window and was most probably the size of the containing CView.
I tried to remove the SetParent call, and the resizing worked correctly. I wonder what is going wrong in resizing and WPF window whose parent is a CView.
I uploaded a sample application illustrating the problem: http://cid-a059807de2e23c43.office.live.com/self.aspx/.Public/ResizeProblem.zip
Please help me.
Thank you.
I had the same problem where after using SetParent any Resize or DragMove on my child window would cause the NewSize to be enormous. My difference is that my main app is WPF where I create & run a child WPF window on a separate thread.
My boss found this link 1 where someone commented on how to set a child's Owner to its parent using reflection:
typeof(System.Windows.Window).InvokeMember(“_ownerHandle”, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.SetField, null, YOUR_CHILD_WINDOW, new object[] { YOUR_PARENT_HANDLE });
After using this code and removing my SetParent my problems went away and was able to set my child window's owner to my main WPF window running on a separate thread.
精彩评论