简体   繁体   English

简单的 c# directX 示例

[英]Simple c# directX example

I try to start c# directX programming with this tutorial: http://www.riemers.net/eng/Tutorials/DirectX/Csharp/Series1/tut2.php I'm using visual Studion Community 2015, my code is like this:我尝试使用本教程开始 c# directX 编程: http ://www.riemers.net/eng/Tutorials/DirectX/Csharp/Series1/tut2.php 我正在使用 Visual Studion Community 2015,我的代码是这样的:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace DirecxTest01
{
    public partial class Form1 : Form
    {
        private Device device;
        public Form1()
        {
            InitializeComponent();
            InitializeDevice();
        }
        private void InitializeDevice()
        {
            PresentParameters presentParams = new PresentParameters();
            presentParams.Windowed = true;
            presentParams.SwapEffect = SwapEffect.Discard;
            device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
        }
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            device.Clear(ClearFlags.Target, Color.DarkSlateBlue, 1.0f, 0);
            device.Present();
        }
    }
}

I also addesd References to all the DirectX dll's.我还添加了对所有 DirectX dll 的引用。 Whe I run the Programm nothing happened, not even the Form Window appers.当我运行程序时什么也没发生,甚至窗体窗口也没有。 No error message, just nothing.没有错误消息,只是什么都没有。 I tried to comment out the DirectX Stuff line by line.我试图逐行注释掉 DirectX Stuff。 Even with this code the Programm hangs up:即使使用此代码,程序也会挂断:

private void InitializeDevice()
    {
        PresentParameters presentParams = new PresentParameters();
        //presentParams.Windowed = true;
        //presentParams.SwapEffect = SwapEffect.Discard;
        //device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
        }

When I out comment当我发表评论

//PresentParameters presentParams = new PresentParameters();

to, at least the Windowas Form apperears.到,至少 Windowas 窗体出现了。

Whats wrong with my code?我的代码有什么问题?

The tutorial you are using is for the deprecated Managed DirectX 1.1 assemblies.您正在使用的教程适用于已弃用的托管 DirectX 1.1 程序集。 They were written for .NET 1.1, and are not compatible with .NET 4.0 or later--although there are some hacks out there to try to make them work.它们是为 .NET 1.1 编写的,与 .NET 4.0 或更高版本不兼容——尽管有一些黑客试图让它们工作。

  • Since the last version of D3DX9 supported by Managed DirectX 1.1 is April 2006, it makes use of a very outdated version of the HLSL compiler.由于 Managed DirectX 1.1 支持的 D3DX9 的最后一个版本是 2006 年 4 月,因此它使用了一个非常过时的 HLSL 编译器版本。
  • The Managed DirectX 1.1 assemblies are 32-bit only, so you cannot use them from an x64 native .NET application ( /platform:anycpu on a Windows x64 system).托管 DirectX 1.1 程序集仅为 32 位,因此您不能从 x64 本机 .NET 应用程序(Windows x64 系统上的/platform:anycpu )中使用它们。 You must build with /platform:x86 and stay within the limits of the 2 GB memory space of 32-bit applications.您必须使用/platform:x86构建并保持在 32 位应用程序的 2 GB 内存空间的限制内。
  • The assemblies only support the legacy DirectX API set, with no support for Direct3D9Ex, Direct3D 10.x, Direct3D 11, Direct2D, DirectWrite, DXGI, D3DX10, D3DX11, XAUDIO2, XACT, XINPUT, etc.程序集仅支持旧版 DirectX API 集,不支持 Direct3D9Ex、Direct3D 10.x、Direct3D 11、Direct2D、DirectWrite、DXGI、D3DX10、D3DX11、XAUDIO2、XACT、XINPUT 等。
  • Since Managed DirectX 2.0 was never released in production form, the Managed DirectX 1.1 assemblies still reflect .NET 1.1 design principles and does not support or make use of .NET 2.0 constructs.由于托管 DirectX 2.0 从未以生产形式发布,托管 DirectX 1.1 程序集仍然反映 .NET 1.1 设计原则,并且不支持或使用 .NET 2.0 构造。
  • Managed DirectX 1.1 while compatible with .NET 2.0 (and the .NET 3.0 and .NET 3.5 extensions of the 2.0 runtime), is not compatible with .NET 4.0托管 DirectX 1.1 虽然与 .NET 2.0(以及 2.0 运行时的 .NET 3.0 和 .NET 3.5 扩展)兼容,但与 .NET 4.0 不兼容
  • The Managed DirectX 1.1 assemblies are only deployed by the legacy DirectSetup and the legacy DirectX SDK.托管 DirectX 1.1 程序集仅由旧版 DirectSetup 和旧版 DirectX SDK 部署。

In short: Don't use them.简而言之:不要使用它们。 Use something more modern like SharpDX, SlimDX, or Unity3D.使用更现代的东西,比如 SharpDX、SlimDX 或 Unity3D。

See DirectX and .NET and Where is the DirectX SDK?请参阅DirectX 和 .NET以及DirectX SDK 在哪里?

I know this is an old thread but I ran into the exact problem and the solution of "don't use DirectX" is not an option because I am working on an existing system without the option of upgrading all the relevant parts using the DirectX dlls.我知道这是一个旧线程,但我遇到了确切的问题,并且“不使用 DirectX”的解决方案不是一个选项,因为我在现有系统上工作,而没有使用 DirectX dll 升级所有相关部分的选项.

Fortunately I found a solution to work with the old DirectX assemblies on a .NET Framework version 4.6.1.幸运的是,我找到了一个解决方案,可以在 .NET Framework 4.6.1 版上使用旧的 DirectX 程序集。

Simply add this in the App.config file (right below the </appSettings> ):只需将其添加到 App.config 文件中(在</appSettings>正下方):

<startup useLegacyV2RuntimeActivationPolicy="true" >
      <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.61"/>
</startup>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM