Jordan
Jordan

Reputation: 9901

How do I resize a WPF control inside a CWnd?

I'm hosting a WPF UserControl inside an MFC CWnd. It works beautifully I now need to figure out how to resize the control with its parent. I've hooked the OnSize and I'm calling GetWindowRect and setting the result to my control like so:

void CChildFrame::OnSize(UINT nType, int cx, int cy)
{
    CRect rect;
    this->GetWindowRect(&rect);

    m_mainControl->Width = rect.Width();
    m_mainControl->Height = rect.Height();
}

Upvotes: 4

Views: 1806

Answers (1)

Jordan
Jordan

Reputation: 9901

Eureka! The solution is to set the HwndSource.SizeToContent to SizeToContent.WidthAndHeight. This seems counter intuitive as SizeToContent is involved with a viewport's ability to size to what it contains, but it worked. My thinking is that it changes the way the control is repainted. The solution as a whole, if anyone wants it is as follows:

Function to create and get a handle to the WPF user control. In this case called MyControl:

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();

    // *** This is the line that fixed my problem.
    m_hwndSource->SizeToContent = SizeToContent::WidthAndHeight;
    m_hwndSource->RootVisual = m_myControl;

    return (HWND) m_hwndSource->Handle.ToPointer();
}

Call GetMyControlHwnd in the OnCreate of the host window. The function creates the parent child relationship itself 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);

    //// create a view to occupy the client area of the frame
    //if (!m_wndView.Create(NULL, NULL, AFX_WS_DEFAULT_VIEW, 
    //  CRect(0, 0, 0, 0), this, AFX_IDW_PANE_FIRST, NULL))
    //{
    //  TRACE0("Failed to create view window\n");
    //  return -1;
    //}

    return 0;
}

When ChildFrame resizes I simply 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 helpful 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;

Upvotes: 5

Related Questions