将非托管 directX 嵌入 C# 形式
我有一个关于在托管环境中运行 directX 应用程序 (C++) 的快速问题。 我正在尝试编写一个 MDI 工具(用 C# 编写),其中父窗口的背景将是 directX (C++) 的嵌入式渲染窗口。
我已经阅读了涉及将 C++ 写入本机 dll 的方法,但是最好能够将 C++ 项目包含在解决方案中(尽管我什至不知道这是否可能)。 不管怎样,如果您知道一些有用的步骤、提示,或者这通常是一个坏主意,请告诉我。 谢谢!
I got a quick question about running a directX application (C++) in a managed environment. I'm attempting to write a MDI tool (in C#) where the background of the parent window would be an embedded render window of directX (C++).
I've read ways that involved writing the C++ into a native dll, however it would be prefered to be able to have the C++ projects included within the solution (I dont even know if that's possible though). Eitherway, if you know of some helpful steps, hints, or if this is a bad idea in general, please let me know. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最简单的方法是将 C++/CLI 项目添加到您的解决方案中。 这样您就可以直接使用 DirectX COM 接口并创建一个易于从 C# 代码调用的托管包装器。 我已经多次采用这种方法,这是迄今为止我尝试过的混合 DirectX 和 .Net 的最简单方法。 托管 DirectX 曾经是一个选项,但它不再受支持,而且它只是完整 COM API 的一个相当小的子集。
The easiest way to do this would be to add a C++/CLI project to your solution. This would then enable you to use the DirectX COM interfaces directly and create a managed wrapper that's easy to call from your C# code. I've taken this approach a few times and it's by far the easiest way of mixing DirectX and .Net that I've ever tried. Managed DirectX used to be an option, but it's no longer supported and it was a fairly small subset of the full COM API anyway.
首先,将 C++ 部分编写在不同的 dll 文件中并不意味着它不能与 C# 项目具有相同的解决方案。
为了使用本机 DX 在托管窗口上渲染,您需要将 HWND 窗口 id(使用 form.WindowId.ToInt32)传递给 C++ D3Ddevice c'tor。 之后,每次使用该设备进行渲染时,它都会在 .NET 窗口上渲染。
为此,您可能需要两个独立的项目 - 一个 C++ dll 和一个 C++ dll。 .NET 项目。 使用 COM 包装器或 p-invoke 将 HWND 传递给 C++ dll。
First of all writing the C++ part in a different dll file doesn't mean that it couldn't be at the same solution as the C# project.
In order to use native DX to render on a managed window you need to pass the HWND window id (use form.WindowId.ToInt32) to the C++ D3Ddevice c'tor. after that each time you'll render using that device it would render on the .NET window.
To do this you probably need two saparate projects - a C++ dll & .NET project. use COM wrapper or p-invoke to pass the HWND to the C++ dll.
如果您不想花太多时间为 DirectX 编写 C++ 代码,可以考虑使用
SlimDX,因为 Managed DirectX 1.0 是不可能的,而 2.0 永远不会离开测试版,稍后会替换为XNA 与 DirectX 本身有很大不同,需要安装 XNA Game Studio
SlimDX 是托管 directx 的 API 和内部结构略有不同,但它很容易使用。 最近发布的版本非常稳定。 我目前正在使用它来编写生产应用程序。
SlimDX
托管 DirectX
If you don't want to spend too much time on writing C++ code for DirectX, you can consider using
SlimDX, since Managed DirectX 1.0 is out of the question, where as 2.0 never leaves the beta and later replace with XNA which has quite different from DirectX itself, and require you to install XNA Game Studio
SlimDX is the opensource version of managed directx with slightly different API and internal structure, but it's easy to use. The recently released version is very stable. I'm currently using it to write a production application.
SlimDX
Managed DirectX