Eureka! The solution is to set HwndSource.SizeToContent to SizeToContent.WidthAndHeight . This seems SizeToContent , as SizeToContent is related to being able to view in size up to what it contains, but it worked. My thinking is that it is changing the way the control is repainted. The solution in general, if someone wants it, is as follows:
The function to create and retrieve a WPF user control handle. In this case, MyControl is called:
HWND CChildFrame::GetMyControlHwnd(HWND a_parent, int a_x, int a_y, int a_width, int a_height) { HWND mainHandle = AfxGetMainWnd()->GetSafeHwnd(); IntPtr testHandle = IntPtr(mainHandle); HwndSource^ test = HwndSource::FromHwnd(testHandle); Global::Bootstrap(IntPtr(mainHandle)); HwndSourceParameters^ sourceParameters = gcnew HwndSourceParameters("MyControl"); sourceParameters->PositionX = a_x; sourceParameters->PositionY = a_y; sourceParameters->Height = a_height; sourceParameters->Width = a_width; sourceParameters->ParentWindow = IntPtr(a_parent); sourceParameters->WindowStyle = WS_VISIBLE | WS_CHILD | WS_MAXIMIZE; m_hwndSource = gcnew HwndSource(*sourceParameters); m_myControl = gcnew MyControl();
Call GetMyControlHwnd in the OnCreate window of the host. The function creates a parent child relation by setting the HwndSource.ParentWindow property.
int CChildFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CMDIChildWnd::OnCreate(lpCreateStruct) == -1) return -1; m_hMyControl = GetMyControlHwnd(this->GetSafeHwnd(), 0, 0, lpCreateStruct->cx, lpCreateStruct->cy);
When the size of the ChildFrame changes, I just change the width and height of the control.
void CChildFrame::OnSize(UINT nType, int cx, int cy) { CRect rect; this->GetWindowRect(&rect); m_myControl->Width = cx; m_myControl->Height = cy; }
I have the following private fields in the header file:
// Fields private: gcroot<HwndSource^> m_hwndSource; gcroot<MyControl^> m_myControl; HWND m_hMyControl;
And its useful to know that this is how you include CLR namespaces in MFC C ++ / CLI code files:
using namespace System; using namespace System::Windows; using namespace System::Windows::Interop;