简单的C#DirectX示例

Ste*_*fen 3 .net c# directx

我尝试使用本教程开始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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我还添加了对所有DirectX dll的引用。当我运行程序时,什么都没发生,甚至窗体窗口也没有。没有错误信息,什么也没有。我试图逐行注释掉DirectX Stuff。即使使用此代码,程序也会挂断:

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

当我发表评论时

//PresentParameters presentParams = new PresentParameters();

至少要出现Windowas表格。

我的代码有什么问题?

Chu*_*urn 5

您正在使用的教程适用于已弃用的Managed DirectX 1.1程序集。它们是为.NET 1.1编写的,并且与.NET 4.0或更高版本不兼容-尽管那里有一些骇客试图使其正常工作。

  • 由于Managed DirectX 1.1支持的D3DX9的最新版本是2006年4月,因此它使用了非常过时的HLSL编译器版本。
  • Managed DirectX 1.1程序集仅是32位,因此您不能从x64本机.NET应用程序(/platform:anycpu在Windows x64系统上)使用它们。您必须使用/platform:x8632位应用程序的2 GB内存空间并在此范围之内。
  • 程序集仅支持旧版DirectX API集,不支持Direct3D9Ex,Direct3D 10.x,Direct3D 11,Direct2D,DirectWrite,DXGI,D3DX10,D3DX11,XAUDIO2,XACT,XINPUT等。
  • 由于Managed DirectX 2.0从未以生产形式发布,因此Managed DirectX 1.1程序集仍然反映.NET 1.1设计原则,并且不支持或使用.NET 2.0构造。
  • 托管DirectX 1.1与.NET 2.0(以及2.0运行时的.NET 3.0和.NET 3.5扩展)兼容时,与.NET 4.0不兼容
  • Managed DirectX 1.1程序集仅由旧版DirectSetup和旧版DirectX SDK部署。

简而言之:不要使用它们。使用更现代的工具,例如SharpDX,SlimDX或Unity3D。

请参阅DirectX和.NETDirectX SDK在哪里?