gpt4 book ai didi

c# - 在 c++/cli 中包装 Interop HwndSource 以在 native c++/MFC 应用程序中使用

转载 作者:太空宇宙 更新时间:2023-11-04 13:34:54 26 4
gpt4 key购买 nike

我有一个用原生 C++ 编写的 MFC 应用程序,它不能使用\clr。我需要在这个 MFC 应用程序的框架内显示一个 WPF 窗口,所以我试图用混合 C++(cli) 制作一个包装器,它包含这个 WPF 页面并且可以被我的 MFC 程序使用。

到目前为止,我得到了包含我的 WPF 窗口的 HwndSource:

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 窗口中。有人知道怎么做吗?我不确定我的代码是否也足够合法,所以如果我错了请纠正我。

谢谢!

最佳答案

好吧,这花了一些时间,我仍然遇到性能问题,但这是一种方法。

首先我得到了 2 个新项目。一种是带有\clr 的 MFC,一种是 native 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;
}

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

关于c# - 在 c++/cli 中包装 Interop HwndSource 以在 native c++/MFC 应用程序中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29918179/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com