如何将MFC窗口句柄强制转换为System.Windows.Window

时间:2013-08-25 23:02:51

标签: c++ wpf mfc interop

我正在尝试在MFC应用程序下托管WPF窗口。我已经创建了一个CCustomEditor类来包装WPF窗口。它有以下创建方法。

BOOL CCustomEditor::Create(LPCTSTR lpszWindowName, DWORD dwStyle,
    const RECT& rect, CWnd* pParentWnd)
{ 
        if (objInterop == NULL) 
        {
            DialogWindow ^myWindow = gcnew DialogWindow(pParentWnd);
            CUnmanagedCustomEditor *objProxy = new CUnmanagedCustomEditor();
            objInterop = objProxy;
            System::String ^strClassName = msclr::interop::marshal_as<System::String ^>(lpszWindowName);
            HwndSourceParameters^ sourceParams = gcnew HwndSourceParameters(strClassName);
            sourceParams->PositionX = rect.left;
            sourceParams->PositionY = rect.top;
            sourceParams->ParentWindow = System::IntPtr(pParentWnd->GetSafeHwnd());
            sourceParams->WindowStyle = dwStyle;
            objProxy->m_hwndSource= gcnew HwndSource(*sourceParams);
            objProxy->m_wpfUC                   = myWindow;     
            objProxy->m_hwndSource->RootVisual  = objProxy->m_wpfUC; 
            // we need to attach a handle since it is a window control
            Attach((HWND) objProxy->m_hwndSource->Handle.ToPointer());  
        }

    return TRUE;
}

我遇到的问题是:

DialogWindow ^myWindow = gcnew DialogWindow(pParentWnd);

我意识到CWnd不能直接作为System.Windows.Window处理,但我不确定需要什么样的转换。

0 个答案:

没有答案