[英]Wrapping Interop HwndSource in c++/cli to be used in native c++ / MFC application
I got an MFC application in native c++, that can not use \\clr. 我有一个本机c ++中的MFC应用程序,它不能使用\\ clr。 I need to show a WPF Window inside a frame in this MFC application, so I am trying to make a wrapper in mixed C++(cli), that contains this WPF page and can be used by my MFC program. 我需要在此MFC应用程序的框架内显示WPF窗口,因此我试图用混合的C ++(cli)制作包装,该包装包含此WPF页面,并且可由我的MFC程序使用。
So far I got the HwndSource to contain my WPF window: 到目前为止,我已经有了HwndSource来包含我的WPF窗口:
WPFPageHost::WPFPageHost(){}
HWND GetHwnd(HWND parent, int x, int y, int width, int height)
{
System::Windows::Interop::HwndSourceParameters^ sourceParams = gcnew System::Windows::Interop::HwndSourceParameters(
"hi" // NAME
);
sourceParams->PositionX = x;
sourceParams->PositionY = y;
sourceParams->Height = height;
sourceParams->Width = width;
sourceParams->ParentWindow = IntPtr(parent);
sourceParams->WindowStyle = WS_VISIBLE | WS_CHILD; // style
source = gcnew System::Windows::Interop::HwndSource(*sourceParams);
gcroot<Frame^> myPage = gcnew Frame();
myPage->Height = height;
myPage->Width = width;
myPage->Background = gcnew SolidColorBrush(Colors::LightGray);
gcroot<MyWindow^> newWindow = gcnew MyWindow;
myPage->Content = newWindow->Content;
source->RootVisual = myPage;
return (HWND) source->Handle.ToPointer();
}
the .h: .h:
#pragma once
#include "resource.h"
#include <vcclr.h>
#include <string.h>
#include "stdafx.h"
using namespace System;
using namespace System::Windows;
using namespace System::Windows::Controls;
using namespace System::Windows::Interop;
using namespace System::Windows::Media;
using namespace MyAPP::WPF;
public class WPFPageHost
{
public:
WPFPageHost();
};
gcroot<HwndSource^> source;
HWND GetHwnd(HWND parent, int x, int y, int width, int height);
Now I need a way to wrap it so I can call this to be added in the MFC Window. 现在,我需要一种包装它的方法,以便可以将其添加到MFC窗口中。 Does someone know how to do it? 有人知道怎么做吗? I am not sure if the code I have is legit enough too, so correct me if I am wrong pls. 我不确定我拥有的代码是否也足够合法,因此,如果我错了,请纠正我。
Thanks! 谢谢!
OK, this took some time and I still have performance issues but here is one way to do it. 好的,这花了一些时间,但我仍然遇到性能问题,但这是解决问题的一种方法。
First of all I got 2 new Projects. 首先,我有两个新项目。 One is a MFC with \\clr and one is a native MFC. 一个是带有\\ clr的MFC,另一个是本机MFC。
In the clr project you make a new class. 在clr项目中,您将创建一个新类。 Here the .h : 这是.h:
#pragma once
class AFX_EXT_CLASS WpfHostWindow : public CWnd
{
DECLARE_DYNAMIC(WpfHostWindow)
public:
WpfHostWindow();
virtual ~WpfHostWindow();
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
#ifdef _MANAGED
gcroot<System::Windows::Controls::Frame^> windowHandle;
#else
intptr_t windowHandle;
#endif
protected:
DECLARE_MESSAGE_MAP()
};
In the .cpp file you generate the hwndsource: 在.cpp文件中,您生成hwndsource:
int WpfHostWindow::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CWnd::OnCreate(lpCreateStruct) == -1)
return -1;
CRect rect;
GetClientRect(&rect);
HwndSourceParameters^ sourceParams= gcnew HwndSourceParameters("CustomHost");
sourceParams->PositionX = 0;
sourceParams->PositionY = 0;
//sourceParams.Height = rect.Height();
//sourceParams.Width = rect.Width();
sourceParams->ParentWindow = (IntPtr)m_hWnd;
sourceParams->WindowStyle = WS_VISIBLE | WS_CHILD;
System::Windows::Interop::HwndSource^ source = gcnew HwndSource(*sourceParams);
source->SizeToContent = System::Windows::SizeToContent::WidthAndHeight;
Frame^ mainFrame = gcnew Frame();
mainFrame->UpdateLayout();
windowHandle = mainFrame;
source->RootVisual = windowHandle;
return 0;
}
In the native MFC project you only need to make a normal CDialog (Or whatever you want) and add a WpfHostWindow Object (Dont forget to reference and stuff). 在本机MFC项目中,您只需要创建一个普通的CDialog(或所需的任何对象)并添加一个WpfHostWindow对象(不要忘记引用和填充)。 In the OnInitDialog() you can now use the .Create Funktion of the WpfHostWindow Object to make the WpfHostWindow as a Child. 在OnInitDialog()中,您现在可以使用WpfHostWindow对象的.Create Funktion将WpfHostWindow设为子级。 Now you can use (theoretically) the native MFC class to make an WPF instance inside your native MFC host. 现在,您可以(理论上)使用本机MFC类在本机MFC主机内创建WPF实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.