Today's my MDX...

Have been read "Rendering Using Simple Techniques" from "Managed DirectX® 9 Kick Start: Graphics and Game Programming" which writed by Tom Miller (His blog is http://blogs.msdn.com/tmiller/)...

问题
# 为什么全屏下的MDX程序,包括系统里面那个Dxdiag的DX检测程序,当点击程序或者屏幕左上角和右上角的时候,程序会退出?(包括一个利用MDX开发的游戏也出现这种情况).
# 怎样设置一下VS2003,使其对MDX的出错信息进行调试?

程序-创建一个Texture化的3D三角形

//创建一个Texture的三角形
//(1)在Form1的构造函数里面设置
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque,true);
this.Load+=new EventHandler(InitGraph);
//(2)按下TAB键可以创建InitGraph(object sender,EventArgs e)
        private void InitGraph(object sender, EventArgs e)
        
{
            PresentParameters pp=new PresentParameters();
            pp.SwapEffect=SwapEffect.Discard;
            pp.Windowed=true;

            device = new Device(0,DeviceType.Hardware,this,CreateFlags.HardwareVertexProcessing,pp);
            vb = new VertexBuffer(typeof(CustomVertex.PositionTextured),36,device,Usage.Dynamic | 

Usage.WriteOnly,
                CustomVertex.PositionTextured.Format,Pool.Default);
            vb.Created+=new EventHandler(vb_Created);
vb_Created(vb,null);
        
}

//(3)按下TAB键可以创建vb_Created了
        private void vb_Created(object sender, EventArgs e)
        
{
            VertexBuffer buf=(VertexBuffer)sender;

            //创建三角形,注意是PositionTextured
            CustomVertex.PositionTextured[] verts=new Microsoft.DirectX.Direct3D.CustomVertex.PositionTextured

[3];
            verts[0]=new Microsoft.DirectX.Direct3D.CustomVertex.PositionTextured(0,4,0,0.0f,0.0f);
            verts[1]=new Microsoft.DirectX.Direct3D.CustomVertex.PositionTextured(-3,0,0,0.0f,1.0f);
            verts[2]=new Microsoft.DirectX.Direct3D.CustomVertex.PositionTextured(3,0,0,1.0f,0.0f);
            buf.SetData(verts,0,LockFlags.None);
            //??LockFlags
        
}

//(4)重写OnPaint
        protected override void OnPaint(PaintEventArgs e)
        
{
            device.Clear(ClearFlags.Target,System.Drawing.Color.Aqua,1.0f,0);
            this.SetCamera();
            this.DrawSence();
            this.Invalidate();
        
}

//(5)完成OnPaint里面几个函数
        private void SetCamera()
        
{
            device.Transform.World = Matrix.RotationAxis(new Vector3(angle/((float)Math.PI*2.0f),angle/((float)

Math.PI*4.0f),
                angle/((float)Math.PI*6.0f)),angle/(float)Math.PI);
            angle += 0.1f;
            device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI/4, this.Width/this.Height, 1.0f, 

100.0f);
            device.Transform.View = Matrix.LookAtLH(new Vector3(0,0,5.0f),new Vector3(),new Vector3(0,1,0));
            device.RenderState.Lighting = false;
        
}


        private void DrawSence()
        
{
            device.BeginScene();
            device.VertexFormat=CustomVertex.PositionTextured.Format;
            device.SetStreamSource(0,vb,0);
            device.DrawPrimitives(PrimitiveType.TriangleList,0,1);
            device.EndScene();
            device.Present();
        
}

//(6)添加Texture
//在InitGraph()的最后
tex=new Texture(device,new Bitmap("T.bmp"),0,Pool.Managed);
//在DrawSence()里面的device.SetStreamSource()后
device.SetTexture(0,tex);
原文地址:https://www.cnblogs.com/caca/p/208700.html