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_015 { 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"> * * */ //设备类 private Device device1 = null; // bool pause = false; //增加此数组 CustomVertex.TransformedColored[] verts; 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位二进制 //参数3,原指定的this,为窗体,现在指定panel1, device1 = new Device(0, DeviceType.Hardware, panel1, 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.VertexFormat = CustomVertex.TransformedColored.Format; device1.DrawUserPrimitives(PrimitiveType.TriangleList, 1, verts); device1.EndScene(); device1.Present(); } private void Form1_Paint(object sender, PaintEventArgs e) { this.Render(); } //此示例是使用panel来显示绘制图形,不再窗体上绘制, public void OnCreateDevice(object sender, EventArgs e) { verts = new CustomVertex.TransformedColored[3]; verts[0].Position = new Vector4(150.0f,50.0f,0.5f,1.0f); verts[0].Color = Color.Green.ToArgb(); verts[1].Position = new Vector4(250.0f, 250.0f, 0.5f, 1.0f); verts[1].Color = Color.Yellow.ToArgb(); verts[2].Position = new Vector4(50.0f, 250.0f, 0.5f, 1.0f); verts[2].Color = Color.LightPink.ToArgb(); } public void OnResetDevice(object sender, EventArgs e) { } private void Form1_Resize(object sender, EventArgs e) { pause = ((this.WindowState == FormWindowState.Minimized) || !this.Visible); } } }
program.cs
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; namespace XNAGame_015 { 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>
form1.design.cs
namespace XNAGame_015 { partial class Form1 { /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { this.panel1 = new System.Windows.Forms.Panel(); this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // panel1 // this.panel1.Location = new System.Drawing.Point(12, 181); this.panel1.Name = "panel1"; this.panel1.Size = new System.Drawing.Size(351, 266); this.panel1.TabIndex = 0; // // button1 // this.button1.Location = new System.Drawing.Point(420, 106); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 1; this.button1.Text = "button1"; this.button1.UseVisualStyleBackColor = true; // // button2 // this.button2.Location = new System.Drawing.Point(420, 229); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(75, 23); this.button2.TabIndex = 2; this.button2.Text = "button2"; this.button2.UseVisualStyleBackColor = true; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(514, 459); this.Controls.Add(this.button2); this.Controls.Add(this.button1); this.Controls.Add(this.panel1); this.Name = "Form1"; this.Text = "Form1"; this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint); this.Resize += new System.EventHandler(this.Form1_Resize); this.ResumeLayout(false); } #endregion private System.Windows.Forms.Panel panel1; private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; } }
【页游开发】