form1.cs

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 XNAGame_013
{
    public partial class Form1 : Form
    {
        /*
         * 1.编写Direct3D程序,首先需要安装direct3d sdk程序。
         * 2.项目引用中,需要添加Microsoft.DirectX,Microsoft.DirectX.Direct3D,Microsoft.Direct3DX,
         * 3.上面的引用,我的电脑的路径是C:\Windows\Microsoft.NET\DirectX for Managed Code\1.0.2902.0
         * 4.vs项目属性中,引用路径要把C:\Program Files (x86)\Microsoft DirectX SDK (June 2010)\Include\,
         * 和C:\Program Files (x86)\Microsoft DirectX SDK (June 2010)\Lib\,都包含进来。
         * 5.在form1.cs中,需要引用 using Microsoft.DirectX,using Microsoft.DirectX.Direct3D;
         * 6.非常重要的一点,此点不解决,窗体不显示,在App.config配置文件中,加入<startup useLegacyV2RuntimeActivationPolicy="true"> 
         * 
         * 数组定义在托管堆中,速度较慢,如果顶点的数组放到图形卡的显存中,可以大大增加绘制图形的速度。
         * 可以使用VertexBuffer类为数组申请存储空间,参数1为顶点类型,参数2为顶点个数,参数3为Device类对象,
         * 参数4一般是0,参数5为顶点格式,参数6,可以是SystemMemory、Default等,一般选择Default,由系统决定位置。
         * 
         * 
         */
        //设备类
        private Device device1 = null;

        //
        bool pause = false;

        //增加这个变量
        VertexBuffer buffer1 = null;

        public Form1()
        {
            InitializeComponent();
        }

        public bool InitializeGraphics()
        {
            try
            {
                PresentParameters param1 = new PresentParameters();
                param1.Windowed = true;  // 非全屏模式,即窗口模式 
                param1.SwapEffect = SwapEffect.Discard;  // 后备缓存模式,新帧显示,旧帧丢弃
                param1.EnableAutoDepthStencil = true;  //自动深度测试
                param1.AutoDepthStencilFormat = DepthFormat.D16; //深度缓存区单元为16位二进制

                device1 = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, param1);
                device1.DeviceReset += new System.EventHandler(this.OnResetDevice);
                this.OnCreateDevice(device1, null);
                this.OnResetDevice(device1, null);

                return true;
            }
            catch (DirectXException ex)
            {
                return false;
            }

        }
        //渲染
        public void Render()
        {
            if (device1 == null) return;   // 设备类没有初始化,为空,不渲染
            if (pause) return; // 暂停,窗体最小化,不可见,不渲染
            device1.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Blue, 1.0f, 0);
            device1.BeginScene();

            //在这里加代码
            device1.SetStreamSource(0, buffer1, 0); //使用显卡缓存中的顶点
            device1.VertexFormat = CustomVertex.TransformedColored.Format; //顶点格式
            device1.DrawPrimitives(PrimitiveType.TriangleList, 0, 6); //绘制立方体

            device1.EndScene();
            device1.Present();
        }


        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            this.Render();
        }
        //在此方法中,添加处理代码
        public void OnCreateDevice(object sender, EventArgs e)
        {
            Device dev = (Device)sender;  //获取设备类
            //新建缓存区域,18个点
            buffer1 = new VertexBuffer(typeof(CustomVertex.TransformedColored), 18, dev, 0, CustomVertex.TransformedColored.Format, Pool.Default);
            buffer1.Created += new System.EventHandler(this.OnCreateVertexBuffer);
            this.OnCreateVertexBuffer(buffer1, null);
        }

        public void OnResetDevice(object sender, EventArgs e)
        {

        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            pause = ((this.WindowState == FormWindowState.Minimized) || !this.Visible);
        }

        //增加此方法,在添加点时,先锁定,添加完毕后,再解锁。
        public void OnCreateVertexBuffer(object sender, EventArgs e)
        {
            CustomVertex.TransformedColored[] verts = (CustomVertex.TransformedColored[])buffer1.Lock(0, 0);

            verts[0].Position = new Vector4(100.0f, 50.0f, 0.5f, 1.0f);     //顶面,左上,点1
            verts[0].Color = Color.Red.ToArgb();

            verts[1].Position = new Vector4(200.0f, 50.0f, 0.5f, 1.0f);     //顶面,右上,点2
            verts[1].Color = Color.Red.ToArgb();

            verts[2].Position = new Vector4(50.0f, 100.0f, 0.5f, 1.0f);     //顶面,左下,点3
            verts[2].Color = Color.Red.ToArgb();

            verts[3].Position = new Vector4(50.0f, 100.0f, 0.5f, 1.0f);     //顶面,左下,点3
            verts[3].Color = Color.Red.ToArgb();

            verts[4].Position = new Vector4(200.0f, 50.0f, 0.5f, 1.0f);     //顶面,右上,点2
            verts[4].Color = Color.Red.ToArgb();

            verts[5].Position = new Vector4(150.0f, 100.0f, 0.5f, 1.0f);    //顶面,右下,点4
            verts[5].Color = Color.Red.ToArgb();

            verts[6].Position = new Vector4(50.0f, 100.0f, 0.5f, 1.0f);     //立面,左上,点3
            verts[6].Color = Color.Green.ToArgb();

            verts[7].Position = new Vector4(150.0f, 100.0f, 0.5f, 1.0f);    //立面,右上,点4
            verts[7].Color = Color.Green.ToArgb();

            verts[8].Position = new Vector4(50.0f, 200.0f, 0.5f, 1.0f);     //立面,左下,点5
            verts[8].Color = Color.Green.ToArgb();

            verts[9].X = 50.0f; //立面,左下,点5
            verts[9].Y = 200.0f;
            verts[9].Z = 0.5f;
            verts[9].Rhw = 1;
            verts[9].Color = Color.Green.ToArgb();

            verts[10].X = 150.0f; //立面,右上,点4
            verts[10].Y = 100.0f;
            verts[10].Z = 0.5f;
            verts[10].Rhw = 1;
            verts[10].Color = Color.Green.ToArgb();

            verts[11].X = 150.0f; //立面,右下,点6
            verts[11].Y = 200.0f;
            verts[11].Z = 0.5f;
            verts[11].Rhw = 1;
            verts[11].Color = Color.Green.ToArgb();

            verts[12].X = 150.0f; //侧面,左上,点4
            verts[12].Y = 100.0f;
            verts[12].Z = 0.5f;
            verts[12].Rhw = 1;
            verts[12].Color = Color.Yellow.ToArgb();

            verts[13].X = 200.0f; //侧面,右上,点2
            verts[13].Y = 50.0f;
            verts[13].Z = 0.5f;
            verts[13].Rhw = 1;
            verts[13].Color = Color.Yellow.ToArgb();

            verts[14].X = 150.0f; //侧面,左下,点6
            verts[14].Y = 200.0f;
            verts[14].Z = 0.5f;
            verts[14].Rhw = 1;
            verts[14].Color = Color.Yellow.ToArgb();

            verts[15].X = 150.0f; //侧面,左下,点6
            verts[15].Y = 200.0f;
            verts[15].Z = 0.5f;
            verts[15].Rhw = 1;
            verts[15].Color = Color.Yellow.ToArgb();

            verts[16].X = 200.0f; //侧面,右上,点2
            verts[16].Y = 50.0f;
            verts[16].Z = 0.5f;
            verts[16].Rhw = 1;
            verts[16].Color = Color.Yellow.ToArgb();

            verts[17].X = 200.0f; //侧面,右下,点7
            verts[17].Y = 150.0f;
            verts[17].Z = 0.5f;
            verts[17].Rhw = 1;
            verts[17].Color = Color.Yellow.ToArgb();

            buffer1.Unlock();
        }
    }
}

program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace XNAGame_013
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            //Application.EnableVisualStyles();
            //Application.SetCompatibleTextRenderingDefault(false);
            //Application.Run(new Form1());
            using (Form1 frm = new Form1())
            {
                if (frm.InitializeGraphics() == false)
                {
                    MessageBox.Show("无法初始化Direct 3D,退出");
                    return;
                }
                frm.Show();
                while (frm.Created == true)
                {
                    frm.Render();
                    Application.DoEvents();
                }
            }
        }
    }
}

app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup useLegacyV2RuntimeActivationPolicy="true"> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
</configuration>

为了便于理解,我画了一张图,

游戏开发 XNAGame-013 绘制静止立方体_xna