【问题标题】:Wrapping Interop HwndSource in c++/cli to be used in native c++ / MFC application在 c++/cli 中包装 Interop HwndSource 以用于本机 c++/MFC 应用程序
【发布时间】: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 窗口中。 有人知道该怎么做吗? 我不确定我的代码是否也足够合法,如果我错了,请纠正我。

谢谢!

【问题讨论】:

  • “我不确定我拥有的代码是否也足够合法” 它可以编译吗?你有运行时错误吗?
  • @πάνταῥεῖ 它的构建没有任何错误。我只是不确定我的代码是否适合这项任务。

标签: c# c++ wpf mfc word-wrap


【解决方案1】:

好的,这需要一些时间,我仍然遇到性能问题,但这是解决问题的一种方法。

首先,我获得了 2 个新项目。一个是带有 \clr 的 MFC,一个是本机 MFC。

在 clr 项目中创建一个新类。 这里是 .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()
};

在 .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;
}

在本机 MFC 项目中,您只需要制作一个普通的 CDialog(或任何您想要的)并添加一个 WpfHostWindow 对象(不要忘记引用和填充)。 在 OnInitDialog() 中,您现在可以使用 WpfHostWindow 对象的 .Create Funktion 将 WpfHostWindow 作为子级。 现在,您可以(理论上)使用本机 MFC 类在本机 MFC 主机中创建 WPF 实例。

【讨论】:

  • 在使用 mfc 上下文菜单时,您是否在 WPF 控件上遇到过任何奇怪的 UI 冻结?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多