【发布时间】:2015-04-28 11:26:43
【问题描述】:
我有一个本地 C++ 中的 MFC 应用程序,它不能使用 \clr。我需要在这个 MFC 应用程序的框架内显示一个 WPF 窗口,所以我试图在混合 C++(cli) 中制作一个包装器,它包含这个 WPF 页面并且可以被我的 MFC 程序使用。
到目前为止,我让 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();
}
.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);
现在我需要一种方法来包装它,以便我可以调用它以将其添加到 MFC 窗口中。 有人知道该怎么做吗? 我不确定我的代码是否也足够合法,如果我错了,请纠正我。
谢谢!
【问题讨论】:
-
“我不确定我拥有的代码是否也足够合法” 它可以编译吗?你有运行时错误吗?
-
@πάνταῥεῖ 它的构建没有任何错误。我只是不确定我的代码是否适合这项任务。